Print even and odd numbers in a given range using recursion
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 = 10
Output:
Even numbers: 2 4 6 8 10
Odd 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++
#include <bits/stdc++.h>
using namespace std;
void Even( int L, int R)
{
if (R < L) {
return ;
}
R % 2 == 0 ? Even(L, R - 2)
: Even(L, R - 1);
if (R % 2 == 0) {
cout << R << " " ;
}
}
void Odd( int L, int R)
{
if (R < L) {
return ;
}
R % 2 == 1 ? Odd(L, R - 2)
: Odd(L, R - 1);
if (R % 2 == 1) {
cout << R << " " ;
}
}
int main()
{
int L = 10, R = 25;
cout << "Even numbers:" ;
Even(L, R);
cout << endl;
cout << "Odd numbers:" ;
Odd(L, R);
}
|
Java
import java.util.*;
class GFG{
static void Even( int L,
int R)
{
if (R < L)
{
return ;
}
if (R % 2 == 0 )
Even(L, R - 2 );
else
Even(L, R - 1 );
if (R % 2 == 0 )
{
System.out.print(R + " " );
}
}
static void Odd( int L,
int R)
{
if (R < L)
{
return ;
}
if (R % 2 == 1 )
Odd(L, R - 2 );
else
Odd(L, R - 1 );
if (R % 2 == 1 )
{
System.out.print(R + " " );
}
}
public static void main(String[] args)
{
int L = 10 , R = 25 ;
System.out.print( "Even numbers:" );
Even(L, R);
System.out.println();
System.out.print( "Odd numbers:" );
Odd(L, R);
}
}
|
Python3
def Even(L, R):
if (R < L):
return
if (R % 2 = = 0 ):
Even(L, R - 2 )
else :
Even(L, R - 1 )
if (R % 2 = = 0 ):
print (R, end = " " )
def Odd(L, R):
if (R < L):
return
if (R % 2 = = 1 ):
Odd(L, R - 2 )
else :
Odd(L, R - 1 )
if (R % 2 = = 1 ):
print (R, end = " " )
if __name__ = = '__main__' :
L = 10
R = 25
print ( "Even numbers:" )
Even(L, R)
print ()
print ( "Odd numbers:" )
Odd(L, R)
|
C#
using System;
class GFG{
static void Even( int L,
int R)
{
if (R < L)
{
return ;
}
if (R % 2 == 0 )
Even(L, R - 2);
else
Even(L, R - 1);
if (R % 2 == 0)
{
Console.Write(R + " " );
}
}
static void Odd( int L,
int R)
{
if (R < L)
{
return ;
}
if (R % 2 == 1 )
Odd(L, R - 2);
else
Odd(L, R - 1);
if (R % 2 == 1)
{
Console.Write(R + " " );
}
}
public static void Main(String[] args)
{
int L = 10, R = 25;
Console.Write( "Even numbers:" );
Even(L, R);
Console.WriteLine();
Console.Write( "Odd numbers:" );
Odd(L, R);
}
}
|
Javascript
<script>
function Even(L, R)
{
if (R < L)
{
return ;
}
if (R % 2 == 0 )
{
Even(L, R - 2);
}
else
{
Even(L, R - 1);
}
if (R % 2 == 0)
{
document.write(R + " " );
}
}
function Odd(L, R)
{
if (R < L)
{
return ;
}
if (R % 2 == 1 )
{
Odd(L, R - 2);
}
else
{
Odd(L, R - 1);
}
if (R % 2 == 1)
{
document.write(R + " " );
}
}
let L = 10;
let R = 25;
document.write( "Even numbers:" );
Even(L, R);
document.write( "<br>" );
document.write( "Odd numbers:" );
Odd(L, R);
</script>
|
Output:
Even numbers:10 12 14 16 18 20 22 24
Odd numbers:11 13 15 17 19 21 23 25
Time Complexity: O(R-L)
Auxiliary Space: O(R-L) for recursive stack space
Last Updated :
06 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...