Print even and odd numbers in a given range using recursion

23 Apr, 2021
Given two integers L and R, the task is to print all the even and odd numbers from L to R using recursion.
Examples:
Input: L = 1, R = 10Output: Even numbers: 2 4 6 8 10Odd numbers: 1 3 5 7 9
Input: L = 10, R = 25 Output: Even numbers:10 12 14 16 18 20 22 24 Odd numbers:11 13 15 17 19 21 23 25
Approach: Follow the steps below to solve the problem using Recursion:
Traverse the range [R, L].
Print the odd elements from the range using recursion using the following recurrence relation:
Odd(L, R) = R % 2 == 1? Odd(L, R – 2) : Odd(L, R – 1) 
Print the even elements from the range using recursion using the following recurrence relation:
Even(L, R) = R % 2 == 0 ? Even(L, R – 2) : Even(L, R – 1) 
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std; // Function to print all the// even numbers from L to Rvoid Even(int L, int R){     // Base case    if (R < L) {        return;    }     // Recurrence relation    R % 2 == 0 ? Even(L, R - 2)               : Even(L, R - 1);     // Check if R is even    if (R % 2 == 0) {        cout << R << " ";    }} // Function to print all the// odd numbers from L to Rvoid Odd(int L, int R){     // Base case    if (R < L) {        return;    }     // Recurrence relation    R % 2 == 1 ? Odd(L, R - 2)               : Odd(L, R - 1);     // Check if R is even    if (R % 2 == 1) {        cout << R << " ";    }} // Driver Codeint main(){    int L = 10, R = 25;    cout << "Even numbers:";     // Print all the    // even numbers    Even(L, R);    cout << endl;     // Print all the    // odd numbers    cout << "Odd numbers:";    Odd(L, R);}
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to print// all the even numbers// from L to Rstatic void Even(int L,                 int R){  // Base case  if (R < L)  {    return;  }   // Recurrence relation  if(R % 2 == 0 )    Even(L, R - 2);  else    Even(L, R - 1);   // Check if R is even  if (R % 2 == 0)  {    System.out.print(R + " ");  }} // Function to print// all the odd numbers// from L to Rstatic void Odd(int L,                int R){  // Base case  if (R < L)  {    return;  }   // Recurrence relation  if(R % 2 == 1 )    Odd(L, R - 2);  else    Odd(L, R - 1);   // Check if R is even  if (R % 2 == 1)  {    System.out.print(R + " ");  }} // Driver Codepublic static void main(String[] args){  int L = 10, R = 25;  System.out.print("Even numbers:");   // Print all the  // even numbers  Even(L, R);  System.out.println();   // Print all the  // odd numbers  System.out.print("Odd numbers:");  Odd(L, R);}} // This code is contributed by Rajput-Ji
# Python3 program to implement# the above approach # Function to print all the# even numbers from L to Rdef Even(L, R):         # Base case    if (R < L):        return     # Recurrence relation    if (R % 2 == 0):        Even(L, R - 2)    else:        Even(L, R - 1)     # Check if R is even    if (R % 2 == 0):        print(R, end = " ") # Function to print all the# odd numbers from L to Rdef Odd(L, R):         # Base case    if (R < L):        return     # Recurrence relation    if (R % 2 == 1):        Odd(L, R - 2)    else:        Odd(L, R - 1)     # Check if R is even    if (R % 2 == 1):        print(R, end = " ") # Driver Codeif __name__ == '__main__':         L = 10    R = 25         print("Even numbers:")     # Print all the    # even numbers    Even(L, R)    print()     # Print all the    # odd numbers    print("Odd numbers:")    Odd(L, R) # This code is contributed by Amit Katiyar
// C# program to implement// the above approachusing System;class GFG{ // Function to print// all the even numbers// from L to Rstatic void Even(int L,                 int R){  // Base case  if (R < L)  {    return;  }   // Recurrence relation  if(R % 2 == 0 )    Even(L, R - 2);  else    Even(L, R - 1);   // Check if R is even  if (R % 2 == 0)  {    Console.Write(R + " ");  }} // Function to print// all the odd numbers// from L to Rstatic void Odd(int L,                int R){  // Base case  if (R < L)  {    return;  }   // Recurrence relation  if(R % 2 == 1 )    Odd(L, R - 2);  else    Odd(L, R - 1);   // Check if R is even  if (R % 2 == 1)  {    Console.Write(R + " ");  }} // Driver Codepublic static void Main(String[] args){  int L = 10, R = 25;  Console.Write("Even numbers:");   // Print all the  // even numbers  Even(L, R);  Console.WriteLine();   // Print all the  // odd numbers  Console.Write("Odd numbers:");  Odd(L, R);}} // This code is contributed by 29AjayKumar
<script> // Java script program to implement// the above approach // Function to print// all the even numbers// from L to Rfunction Even(L, R){         // Base case    if (R < L)    {        return;    }         // Recurrence relation    if (R % 2 == 0 )    {        Even(L, R - 2);    }    else    {        Even(L, R - 1);    }         // Check if R is even    if (R % 2 == 0)    {        document.write(R + " ");    }} // Function to print// all the odd numbers// from L to Rfunction Odd(L, R){         // Base case    if (R < L)    {        return;    }         // Recurrence relation    if (R % 2 == 1 )    {        Odd(L, R - 2);    }    else    {        Odd(L, R - 1);    }         // Check if R is even    if (R % 2 == 1)    {        document.write(R + " ");    }} // Driver Codelet L = 10;let R = 25;document.write("Even numbers:"); // Print all the// even numbersEven(L, R);document.write("<br>"); // Print all the// odd numbersdocument.write("Odd numbers:");Odd(L, R); // This code is contributed by sravan kumar G </script>
Even numbers:10 12 14 16 18 20 22 24 
Odd numbers:11 13 15 17 19 21 23 25
 
Time Complexity: O(N)Auxiliary Space: O(1)
Rajput-Ji
29AjayKumar
amit143katiyar
sravankumar8128
Number Divisibility
Mathematical
Recursion
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.