Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
If we examine closely at this problem, we can see that the concept of “loop” is to track some counter value, such as “i=0″ till I = higher”. So, if we aren’t permitted to use loops, how can we track something in Python?
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.
Examples:
Example1:
Input:
Enter some upper limit range = 11
Output:
The numbers from 1 to 11 without using loops : 1 2 3 4 5 6 7 8 9 10 11
Example2:
Input:
Enter some upper limit range = 28
Output:
The numbers from 1 to 28 without using loops : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Program to Print Numbers in a Range (1,upper) Without Using any Loops/Recursive Approach
Below are the ways to print all the numbers in the range from 1 to upper without using any loops or by using recursive approach.
Method #1:Using Recursive function(Static Input)
Approach:
- Give the upper limit range using static input.
- Create a recursive function.
- Create a basic case for that function in when the integer is greater than zero.
- If the number is more than zero, call the function again with the input set to the number -1.
- Print the number.
- Exit of program.
Below is the implementation:
# function which prints numbers from 1 to gievn upper limit range # without loop/by using recursion def printNumbers(upper_limit): # checking if th upper limit value is greater than 0 using if statement if(upper_limit > 0): # If the number is more than zero, call the function again with # the input set to the number -1. printNumbers(upper_limit-1) # Print the upper limit print(upper_limit) # giveen upper limt range upper_limit = 28 print('The numbers from 1 to', upper_limit, 'without using loops : ') # passing the given upper limit range to printNumbers printNumbers(upper_limit)
Output:
The numbers from 1 to 28 without using loops : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Method #2:Using Recursive function(User Input)
Approach:
- Scan the upper limit range using int(input()).
- Create a recursive function.
- Create a basic case for that function in when the integer is greater than zero.
- If the number is more than zero, call the function again with the input set to the number -1.
- Print the number.
- Exit of program.
Below is the implementation:
# function which prints numbers from 1 to gievn upper limit range # without loop/by using recursion def printNumbers(upper_limit): # checking if th upper limit value is greater than 0 using if statement if(upper_limit > 0): # If the number is more than zero, call the function again with # the input set to the number -1. printNumbers(upper_limit-1) # Print the upper limit print(upper_limit) # giveen upper limt range upper_limit = int(input('Enter some upper limit range = ')) print('The numbers from 1 to', upper_limit, 'without using loops : ') # passing the given upper limit range to printNumbers printNumbers(upper_limit)
Output:
Enter some upper limit range = 11 The numbers from 1 to 11 without using loops : 1 2 3 4 5 6 7 8 9 10 11
Explanation:
- The user must enter the range’s top limit.
- This value is supplied to the recursive function as an argument.
- The recursive function’s basic case is that number should always be bigger than zero.
- If the number is greater than zero, the function is called again with the argument set to the number minus one.
- The number has been printed.
- The recursion will continue until the number is less than zero.