;

# C# Program To Print even or odd numbers in a given range using Recursion

###### Tutorialsrack 11/08/2019 C# Programs

In this C# program, we will learn how to write a program to print Even or Odd numbers in a given range using Recursion.

Here is the code of the program to print Even or Odd numbers in a given range using Recursion:

##### Code - C# Program To print Even or Odd numbers in a given range using Recursion.
``````using System;

namespace TutorialsrackPrograms
{
class Program
{
//C# Program To Print Print even or odd numbers in a given range using Recursion
static void Main(string[] args)
{
int number;
Console.Write("\n\n Using Recursion : Print even or odd numbers in a given range :\n");
Console.Write("-------------------------------------------------------------\n");

Console.Write(" Enter the Number to Print Starting from 1 to ");

Console.WriteLine("\n All Even Numbers From 1 to {0} are: ", number);
EvenAndOdd(2, number);//call the function EvenAndOdd for even numbers

Console.WriteLine("\n\n All Odd Numbers From 1 to {0} are: ", number);
EvenAndOdd(1, number);// call the function EvenAndOdd for odd numbers
Console.WriteLine("\n\n");

}

static void EvenAndOdd(int startVal, int number)
{
if (startVal > number)
return;
Console.Write(" {0}  ", startVal);
EvenAndOdd(startVal + 2, number);//calling the function EvenAndOdd itself recursively
}
}
}``````
##### Output

Using Recursion: Print even or odd numbers in a given range :
-------------------------------------------------------------
Enter the Number to Print Starting from 1 to 30

All Even Numbers From 1 to 30 are:
2  4  6  8  10  12  14  16  18  20  22  24  26  28  30

All Odd Numbers From 1 to 30 are:
1  3  5  7  9  11  13  15  17  19  21  23  25  27  29