Learn about Print 1 to 10 numbers without Loop in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view of what’s actually happening.
Print 1 to 10 numbers without Loop in Python
So, if we are not permitted to use loops, how can we track something in any programming language?
One option is to use recursion, however, we must be careful with the terminating condition. Here’s a solution that uses recursion to output numbers.
# Python program to print numbers from 1 to 10 def print_num(n): if n > 0: print_num(n - 1) print(n, end = ' ') print('Numbers from 1 to 10 are:') print_num(10)
Numbers from 1 to 10 are:
1 2 3 4 5 6 7 8 9 10
Hope the above code works for you and Refer to the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code: Python program to print numbers from 1 to 10