When it is required to print the numbers in a given range without using any loop, a method is defined, that keeps displays numbers from higher range by uniformly decrementing it by one after every print statement.
Below is the demonstration of the same −
Example
def print_nums(upper_num):
if(upper_num>0):
print_nums(upper_num-1)
print(upper_num)
upper_lim = 6
print("The upper limit is :")
print(upper_lim)
print("The numbers are :")
print_nums(upper_lim)Output
The upper limit is : 6 The numbers are : 1 2 3 4 5 6
Explanation
A method named ‘print_nums’ is defined.
It checks if the upper limit is greater than 0.
If so, keeps displaying the elements.
After every display, the upper range value is decremented by 1.
Outside the function, a value for upper limit is defined.
This method is called by passing the parameter.
The output is displayed on the console.