def print_range(start, end):
if start > end:
return
print(start)
print_range(start + 1, end)
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print_range(start, end)
Code Explanation:
def print_range(start, end):
A function named print_range is defined.
It takes two arguments:
start: The starting number.
end: The ending number.
2. Base Case for Recursion
if start > end:
return
This is the base case of the recursion, which stops the function from continuing further:
If start is greater than end, the function simply returns without doing anything.
3. Print the Current Number
print(start)
If start is not greater than end, the current value of start is printed.
4. Recursive Call
print_range(start + 1, end)
After printing the current number, the function calls itself with:
start + 1: The next number in the sequence.
end: The same ending number.
This continues until the base case (start > end) is met.
5. Input from User
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
The user is prompted to input the start and end values for the range.
int(input(...)) ensures the inputs are converted to integers.
6. Call the Function
print_range(start, end)
The print_range function is called with the user-provided start and end values.
Recursive Process (How it Works)
The function prints the current value of start.
It calls itself with the next number (start + 1).
This continues until start > end, at which point the function stops
source code --> clcoding.com
0 Comments:
Post a Comment