This is a Python Program to print all numbers in a range without using loops.
The program takes in the upper limit and prints all numbers within the given range using recursive function.
1. Define a recursive function.
2. Define a base case for that function that the number should be greater than zero.
3. If number is greater than 0, call the function again with the argument as the number minus 1.
4. Print the number.
5. Exit.
Here is source code of the Python Program to print all numbers in a range without using loops. The program output is also shown below.
def printno(upper): if(upper>0): printno(upper-1) print(upper) upper=int(input("Enter upper limit: ")) printno(upper)
1. User must enter the upper limit of the range.
2. This value is passed as an argument for the recursive function.
3. The base case for the recursive function is that number should always be greater than 0.
4. If number is greater than 0, function is called again with the argument as the number minus 1.
5. The number is printed.
6. The recursion continues until the number becomes lesser than 0.
Case 1: Enter upper limit: 5 1 2 3 4 5 Case 2: Enter upper limit: 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Apply for Programming Internship
- Apply for Python Internship
- Check Information Technology Books
- Check Python Books
- Practice Programming MCQs