Posted on by Kalkicode
Code Recursion

# Print even and odd numbers in given range using recursion

The given problem focuses on printing even and odd numbers within a specified range using recursion. Recursion is a programming technique in which a function calls itself to solve a problem in smaller instances. In this case, the problem involves printing even and odd numbers within a given range.

## Problem Statement

The problem is to design a program that takes a range (starting and ending numbers) as input and then prints all the even numbers followed by all the odd numbers within that range. The program should use recursion to achieve this.

## Example

Let's consider the input range `(1-10)`. The program should output:

``````Given Range (1-10)
Even Number: 2 4 6 8 10
Odd Number: 1 3 5 7 9``````

## Idea to Solve

The problem can be solved using recursion by following these steps:

1. Define a recursive function `printValue(int num, int last)` to print numbers within a given range.
2. Within this function, check if `num` is less than or equal to `last`.
3. If yes, print `num` (since it's within the range) and call the `printValue` function recursively with `num + 2` and `last`.
4. Define another function `printEvenOdd(int first, int last)` to determine whether the first number in the range is even or odd.
5. If the first number is even, call `printValue` for even and odd numbers separately with `first` and `first + 1` as starting points.
6. If the first number is odd, call `printValue` for odd and even numbers separately with `first` and `first + 1`.
7. The base case for recursion in both `printValue` and `printEvenOdd` functions is when `first` becomes greater than `last`.

## Pseudocode

``````printValue(num, last):
if num <= last:
print num
printValue(num + 2, last)

printEvenOdd(first, last):
if first > last:
return
print "Given Range (", first, "-", last, ")"
if first is even:
print "Even Number :"
printValue(first, last)
print "Odd Number :"
printValue(first + 1, last)
else:
print "Even Number :"
printValue(first + 1, last)
print "Odd Number :"
printValue(first, last)

main:
printEvenOdd(1, 10)
printEvenOdd(53, 74)``````

## Algorithm

1. Implement the `printValue` function as described in the pseudocode.
2. Implement the `printEvenOdd` function as described in the pseudocode.
3. In the `main` function, call `printEvenOdd` with the desired input ranges.

## Program List

``````// C program for
// Print even and odd numbers in given range using recursion
#include <stdio.h>

// Recursivaly print given number
void printValue(int num, int last)
{
if (num <= last)
{
// Display number
printf(" %d", num);
// Recursively call
printValue(num + 2, last);
}
}
void printEvenOdd(int first, int last)
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
printf("\n Given Range (%d-%d)", first, last);
// Check that given range first value is even or odd
if (first % 2 == 0)
{
printf("\n Even Number :");
// Print all even number
printValue(first, last);
printf("\n Odd Number :");
// Print all odd number
printValue(first + 1, last);
}
else
{
printf("\n Even Number :");
// Print all even number
printValue(first + 1, last);
printf("\n Odd Number :");
// Print all odd number
printValue(first, last);
}
}
int main(int argc, char const *argv[])
{
// Test Case
printEvenOdd(1, 10);
printEvenOdd(53, 74);
return 0;
}``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````// Java program for
// Print even and odd numbers in given range using recursion
public class Series
{
// Recursivaly print given number
public void printValue(int num, int last)
{
if (num <= last)
{
// Display number
System.out.print(" " + num);
// Recursively call
printValue(num + 2, last);
}
}
public void printEvenOdd(int first, int last)
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
System.out.print("\n Given Range (" + first + "-" + last + ")");
// Check that given range first value is even or odd
if (first % 2 == 0)
{
System.out.print("\n Even Number :");
// Print all even number
printValue(first, last);
System.out.print("\n Odd Number :");
// Print all odd number
printValue(first + 1, last);
}
else
{
System.out.print("\n Even Number :");
// Print all even number
printValue(first + 1, last);
System.out.print("\n Odd Number :");
// Print all odd number
printValue(first, last);
}
}
public static void main(String[] args)
{
// Test Case
}
}``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Print even and odd numbers in given range using recursion
class Series
{
public:
// Recursivaly print given number
void printValue(int num, int last)
{
if (num <= last)
{
// Display number
cout << " " << num;
// Recursively call
this->printValue(num + 2, last);
}
}
void printEvenOdd(int first, int last)
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
cout << "\n Given Range (" << first << "-" << last << ")";
// Check that given range first value is even or odd
if (first % 2 == 0)
{
cout << "\n Even Number :";
// Print all even number
this->printValue(first, last);
cout << "\n Odd Number :";
// Print all odd number
this->printValue(first + 1, last);
}
else
{
cout << "\n Even Number :";
// Print all even number
this->printValue(first + 1, last);
cout << "\n Odd Number :";
// Print all odd number
this->printValue(first, last);
}
}
};
int main()
{
// Test Case
return 0;
}``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````// Include namespace system
using System;
// Csharp program for
// Print even and odd numbers in given range using recursion
public class Series
{
// Recursivaly print given number
public void printValue(int num, int last)
{
if (num <= last)
{
// Display number
Console.Write(" " + num);
// Recursively call
this.printValue(num + 2, last);
}
}
public void printEvenOdd(int first, int last)
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
Console.Write("\n Given Range (" + first + "-" + last + ")");
// Check that given range first value is even or odd
if (first % 2 == 0)
{
Console.Write("\n Even Number :");
// Print all even number
this.printValue(first, last);
Console.Write("\n Odd Number :");
// Print all odd number
this.printValue(first + 1, last);
}
else
{
Console.Write("\n Even Number :");
// Print all even number
this.printValue(first + 1, last);
Console.Write("\n Odd Number :");
// Print all odd number
this.printValue(first, last);
}
}
public static void Main(String[] args)
{
// Test Case
}
}``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````<?php
// Php program for
// Print even and odd numbers in given range using recursion
class Series
{
// Recursivaly print given number
public	function printValue(\$num, \$last)
{
if (\$num <= \$last)
{
// Display number
echo(" ".\$num);
// Recursively call
\$this->printValue(\$num + 2, \$last);
}
}
public	function printEvenOdd(\$first, \$last)
{
if (\$first > \$last)
{
// Range are invalid
return;
}
// Display the value of given range
echo("\n Given Range (".\$first.
"-".\$last.
")");
// Check that given range first value is even or odd
if (\$first % 2 == 0)
{
echo("\n Even Number :");
// Print all even number
\$this->printValue(\$first, \$last);
echo("\n Odd Number :");
// Print all odd number
\$this->printValue(\$first + 1, \$last);
}
else
{
echo("\n Even Number :");
// Print all even number
\$this->printValue(\$first + 1, \$last);
echo("\n Odd Number :");
// Print all odd number
\$this->printValue(\$first, \$last);
}
}
}

function main()
{
// Test Case
}
main();``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````// Node JS program for
// Print even and odd numbers in given range using recursion
class Series
{
// Recursivaly print given number
printValue(num, last)
{
if (num <= last)
{
// Display number
process.stdout.write(" " + num);
// Recursively call
this.printValue(num + 2, last);
}
}
printEvenOdd(first, last)
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
process.stdout.write("\n Given Range (" + first + "-" + last + ")");
// Check that given range first value is even or odd
if (first % 2 == 0)
{
process.stdout.write("\n Even Number :");
// Print all even number
this.printValue(first, last);
process.stdout.write("\n Odd Number :");
// Print all odd number
this.printValue(first + 1, last);
}
else
{
process.stdout.write("\n Even Number :");
// Print all even number
this.printValue(first + 1, last);
process.stdout.write("\n Odd Number :");
// Print all odd number
this.printValue(first, last);
}
}
}

function main()
{
// Test Case
}
main();``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````#  Python 3 program for
#  Print even and odd numbers in given range using recursion
class Series :
#  Recursivaly print given number
def printValue(self, num, last) :
if (num <= last) :
#  Display number
print(" ", num, end = "")
#  Recursively call
self.printValue(num + 2, last)

def printEvenOdd(self, first, last) :
if (first > last) :
#  Range are invalid
return

#  Display the value of given range
print("\n Given Range (", first ,"-", last ,")", end = "")
#  Check that given range first value is even or odd
if (first % 2 == 0) :
print("\n Even Number :", end = "")
#  Print all even number
self.printValue(first, last)
print("\n Odd Number :", end = "")
#  Print all odd number
self.printValue(first + 1, last)
else :
print("\n Even Number :", end = "")
#  Print all even number
self.printValue(first + 1, last)
print("\n Odd Number :", end = "")
#  Print all odd number
self.printValue(first, last)

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### input

`````` Given Range ( 1 - 10 )
Even Number :  2  4  6  8  10
Odd Number :  1  3  5  7  9
Given Range ( 53 - 74 )
Even Number :  54  56  58  60  62  64  66  68  70  72  74
Odd Number :  53  55  57  59  61  63  65  67  69  71  73``````
``````#  Ruby program for
#  Print even and odd numbers in given range using recursion
class Series
#  Recursivaly print given number
def printValue(num, last)
if (num <= last)
#  Display number
print(" ", num)
#  Recursively call
self.printValue(num + 2, last)
end

end

def printEvenOdd(first, last)
if (first > last)
#  Range are invalid
return
end

#  Display the value of given range
print("\n Given Range (", first ,"-", last ,")")
#  Check that given range first value is even or odd
if (first % 2 == 0)
print("\n Even Number :")
#  Print all even number
self.printValue(first, last)
print("\n Odd Number :")
#  Print all odd number
self.printValue(first + 1, last)
else
print("\n Even Number :")
#  Print all even number
self.printValue(first + 1, last)
print("\n Odd Number :")
#  Print all odd number
self.printValue(first, last)
end

end

end

def main()
#  Test Case
end

main()``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````// Scala program for
// Print even and odd numbers in given range using recursion
class Series()
{
// Recursivaly print given number
def printValue(num: Int, last: Int): Unit = {
if (num <= last)
{
// Display number
print(" " + num);
// Recursively call
printValue(num + 2, last);
}
}
def printEvenOdd(first: Int, last: Int): Unit = {
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
print("\n Given Range (" + first + "-" + last + ")");
// Check that given range first value is even or odd
if (first % 2 == 0)
{
print("\n Even Number :");
// Print all even number
printValue(first, last);
print("\n Odd Number :");
// Print all odd number
printValue(first + 1, last);
}
else
{
print("\n Even Number :");
// Print all even number
printValue(first + 1, last);
print("\n Odd Number :");
// Print all odd number
printValue(first, last);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Series = new Series();
// Test Case
}
}``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````
``````// Swift 4 program for
// Print even and odd numbers in given range using recursion
class Series
{
// Recursivaly print given number
func printValue(_ num: Int, _ last: Int)
{
if (num <= last)
{
// Display number
print(" ", num, terminator: "");
// Recursively call
self.printValue(num + 2, last);
}
}
func printEvenOdd(_ first: Int, _ last: Int)
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
print("\n Given Range (", first ,"-", last ,")", terminator: "");
// Check that given range first value is even or odd
if (first % 2 == 0)
{
print("\n Even Number :", terminator: "");
// Print all even number
self.printValue(first, last);
print("\n Odd Number :", terminator: "");
// Print all odd number
self.printValue(first + 1, last);
}
else
{
print("\n Even Number :", terminator: "");
// Print all even number
self.printValue(first + 1, last);
print("\n Odd Number :", terminator: "");
// Print all odd number
self.printValue(first, last);
}
}
}
func main()
{
// Test Case
}
main();``````

#### input

`````` Given Range ( 1 - 10 )
Even Number :  2  4  6  8  10
Odd Number :  1  3  5  7  9
Given Range ( 53 - 74 )
Even Number :  54  56  58  60  62  64  66  68  70  72  74
Odd Number :  53  55  57  59  61  63  65  67  69  71  73``````
``````// Kotlin program for
// Print even and odd numbers in given range using recursion
class Series
{
// Recursivaly print given number
fun printValue(num: Int, last: Int): Unit
{
if (num <= last)
{
// Display number
print(" " + num);
// Recursively call
this.printValue(num + 2, last);
}
}
fun printEvenOdd(first: Int, last: Int): Unit
{
if (first > last)
{
// Range are invalid
return;
}
// Display the value of given range
print("\n Given Range (" + first + "-" + last + ")");
// Check that given range first value is even or odd
if (first % 2 == 0)
{
print("\n Even Number :");
// Print all even number
this.printValue(first, last);
print("\n Odd Number :");
// Print all odd number
this.printValue(first + 1, last);
}
else
{
print("\n Even Number :");
// Print all even number
this.printValue(first + 1, last);
print("\n Odd Number :");
// Print all odd number
this.printValue(first, last);
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

#### input

`````` Given Range (1-10)
Even Number : 2 4 6 8 10
Odd Number : 1 3 5 7 9
Given Range (53-74)
Even Number : 54 56 58 60 62 64 66 68 70 72 74
Odd Number : 53 55 57 59 61 63 65 67 69 71 73``````

## Time Complexity

The time complexity of this program is O((last - first) / 2) because the `printValue` function is called recursively with an increment of 2 in each step. Thus, the number of steps taken is roughly half of the range width.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post