Program to Print Odd Numbers in Given Range Using Recursion

Python Program to Print Odd Numbers in Given Range Using Recursion

In the previous article, we have discussed Python Program to Print Even Numbers in Given Range Using Recursion

Given the upper limit and the task is to print the odd numbers in a given range (from 1 to given limit).

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

 Given Upper Limit = 45

Output:

The Odd Numbers in a given range 1 and 45 are :
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45

Example2:

Input:

Given Upper Limit = 20

Output:

The Odd Numbers in a given range 1 and 20 are :
1 3 5 7 9 11 13 15 17 19

Program to Print Odd Numbers in Given Range Using Recursion in Python

Below are the ways to print the odd numbers in a given range in python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the lower limit as 1 and store it in a variable.
  • Give the upper limit as static input and store it in another variable.
  • Pass the given lower and upper limits as the arguments to the odd_range function.
  • Create a recursive function to say odd_range which takes the given lower and upper limits as arguments and returns the odd numbers in a given range using recursion.
  • Check if the given lower limit value is greater than the upper limit using the if conditional statement.
  • If the statement is true, then return.
  • After the return statement, print the given lower limit separated by spaces.
  • Return odd_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
  • Print the Odd Numbers in a given lower and upper limit range.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say odd_range which takes the given lower and upper
# limits as arguments and returns the odd numbers in a given range using recursion.


def odd_range(gvnlowr_lmt, gvnuppr_lmt):
    # Check if the given lower limit value is greater than the upper limit using the if
    # conditional statement.
    if gvnlowr_lmt > gvnuppr_lmt:
        # If the statement is true, then return.
        return
    # After the return statement, print the given lower limit separated by spaces.
    print(gvnlowr_lmt, end=" ")
    # Return odd_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
    return odd_range(gvnlowr_lmt+2, gvnuppr_lmt)


# Give the lower limit as 1 and store it in a variable.
gvnlowr_lmt = 1
# Give the upper limit as static input and store it in another variable.
gvnuppr_lmt = 45
# Pass the given lower and upper limits as the arguments to odd_range function.
# Print the Odd Numbers in a given range.
print("The Odd Numbers in a given range",
      gvnlowr_lmt, "and", gvnuppr_lmt, "are :")
odd_range(gvnlowr_lmt, gvnuppr_lmt)

Output:

The Odd Numbers in a given range 1 and 45 are :
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45

Method #2: Using Recursion (User Input)

Approach:

  • Give the lower limit as 1 and store it in a variable.
  • Give the upper limit as user input using the int(input()) function and store it in another variable.
  • Pass the given lower and upper limits as the arguments to the odd_range function.
  • Create a recursive function to say odd_range which takes the given lower and upper limits as arguments and returns the odd numbers in a given range using recursion.
  • Check if the given lower limit value is greater than the upper limit using the if conditional statement.
  • If the statement is true, then return.
  • After the return statement, print the given lower limit separated by spaces.
  • Return odd_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
  • Print the Odd Numbers in a given lower and upper limit range.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say odd_range which takes the given lower and upper
# limits as arguments and returns the odd numbers in a given range using recursion.


def odd_range(gvnlowr_lmt, gvnuppr_lmt):
    # Check if the given lower limit value is greater than the upper limit using the if
    # conditional statement.
    if gvnlowr_lmt > gvnuppr_lmt:
        # If the statement is true, then return.
        return
    # After the return statement, print the given lower limit separated by spaces.
    print(gvnlowr_lmt, end=" ")
    # Return odd_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
    return odd_range(gvnlowr_lmt+2, gvnuppr_lmt)


# Give the lower limit as 1 and store it in a variable.
gvnlowr_lmt = 1
# Give the upper limit as user input using the int(input()) function and
# store it in another variable.
gvnuppr_lmt = int(input("Enter some random number = "))
# Pass the given lower and upper limits as the arguments to odd_range function.
# Print the Odd Numbers in a given range.
print("The Odd Numbers in a given range",
      gvnlowr_lmt, "and", gvnuppr_lmt, "are :")
odd_range(gvnlowr_lmt, gvnuppr_lmt)

Output:

Enter some random number = 20
The Odd Numbers in a given range 1 and 20 are :
1 3 5 7 9 11 13 15 17 19

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.