Program to Print Even Numbers in Given Range Using Recursion

Python Program to Print Even Numbers in Given Range Using Recursion

In the previous article, we have discussed Python Program to Divide Two Numbers Using Recursion

Given the Lower and upper limits as range and the task is to print the even numbers in a given range.

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 Lower Limit = 30
 Given Upper Limit = 50

Output:

The Even numbers in a given range 30 and 50 are :
30 32 34 36 38 40 42 44 46 48 50

Example2:

Input:

Given Lower Limit = 60
Given Upper Limit = 100

Output:

The Even numbers in a given range 60 and 100 are :
60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

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

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

Method #1: Using Recursion (Static Input)

Approach:

  • Give the lower limit as static input 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 even_range function.
  • Create a recursive function to say even_range which takes the given lower and upper limits as arguments and returns the even 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 even_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
  • Print the Even Numbers in a given lower and upper limit range.
  • The Exit of the Program.

Below is the implementation:

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


def even_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 even_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
    return even_range(gvnlowr_lmt+2, gvnuppr_lmt)


# Give the lower limit as static input and store it in a variable.
gvnlowr_lmt = 30
# Give the upper limit as static input and store it in another variable.
gvnuppr_lmt = 50
# Pass the given lower and upper limits as the arguments to even_range function.
# Print the even Numbers in a given range.
print("The Even numbers in a given range",
      gvnlowr_lmt, "and", gvnuppr_lmt, "are :")
even_range(gvnlowr_lmt, gvnuppr_lmt)

Output:

The Even numbers in a given range 30 and 50 are :
30 32 34 36 38 40 42 44 46 48 50

Method #2: Using Recursion (User Input)

Approach:

  • Give the lower limit as user input using the int(input()) function 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 even_range function.
  • Create a recursive function to say even_range which takes the given lower and upper limits as arguments and returns the even 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 even_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
  • Print the Even Numbers in a given lower and upper limit range.
  • The Exit of the Program.

Below is the implementation:

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


def even_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 even_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.
    return even_range(gvnlowr_lmt+2, gvnuppr_lmt)


# Give the lower limit as user input using the int(input()) function
# and store it in a variable.
gvnlowr_lmt = int(input("Enter some random number = "))
# 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 even_range function.
# Print the even Numbers in a given range.
print("The Even numbers in a given range",
      gvnlowr_lmt, "and", gvnuppr_lmt, "are :")
even_range(gvnlowr_lmt, gvnuppr_lmt)

Output:

Enter some random number = 60
Enter some random number = 100
The Even numbers in a given range 60 and 100 are :
60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.