w3resource

C#: Print even or odd numbers in a given range

C# Sharp Recursion : Exercise-6 with Solution

Write a program in C# Sharp to print even or odd numbers in a given range using recursion.

Calculating a Even Numbers:

Calculating a Even Numbers

Even Numbers between 1 to 100:

Even Numbers between 1 to 100

Calculating a Odd Numbers:

Calculating a Odd Numbers

Odd Numbers between 1 to 100:

Odd Numbers between 1 to 100

Sample Solution:-

C# Sharp Code:

using System;

// Class definition named 'RecExercise6'
class RecExercise6
{
    // Main method, the entry point of the program
    public static void Main()
    {
        int n;

        // Display a description of the program
        Console.Write("\n\n Recursion : Print even or odd numbers in a given range :\n");
        Console.Write("-------------------------------------------------------------\n");

        // Prompt user to input a range
        Console.Write(" Input the range to print starting from 1 : ");
        n = Convert.ToInt32(Console.ReadLine());

        // Display even numbers from 1 to n
        Console.WriteLine("\n All even numbers from 1 to {0} are : ", n);
        EvenAndOdd(2, n); // Call the function EvenAndOdd for even numbers

        // Display odd numbers from 1 to n
        Console.WriteLine("\n\n All odd numbers from 1 to {0} are : ", n);
        EvenAndOdd(1, n); // Call the function EvenAndOdd for odd numbers
        Console.WriteLine("\n\n");

        return;
    }

    // Recursive function to print even or odd numbers in a given range
    static void EvenAndOdd(int stVal, int n)
    {
        // Base case: If starting value exceeds the range, return
        if (stVal > n)
            return;

        // Print the current value (even or odd)
        Console.Write(" {0}  ", stVal);

        // Recursive call: Increment stVal by 2 and call EvenAndOdd function
        EvenAndOdd(stVal + 2, n);
    }
}

Sample Output:

 Recursion : Print even or odd numbers in a given range :                                                     
-------------------------------------------------------------                                                 
 Input the range to print starting from 1 : 20                                                                
                                                                                                              
 All even numbers from 1 to 20 are :                                                                          
 2   4   6   8   10   12   14   16   18   20                                                                  
                                                                                                              
 All odd numbers from 1 to 20 are :                                                                           
 1   3   5   7   9   11   13   15   17   19

Flowchart :

Flowchart: C# Sharp Exercises - Print even or odd numbers in a given range

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to count the number of digits in a number using recursion.
Next: Write a program in C# Sharp to check whether a number is prime or not using recursion.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.