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.
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.
Here is source code of the Python Program to check if a number is a prime number. 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.