C# Program to Print All Natural Numbers Using Recursion

First N natural numbers

C# program to print all the natural numbers from 1 to N using recursion has been shown here.






1. C# Program & output to Print First N Natural Numbers using recursion

Code has been copied
/*****************************************************************
                   alphabetacoder.com
C# program to print all natual numbers from 1 to n using recursion
*******************************************************************/

using System;

namespace PrintNaturalNumber {
    class Program {
        // recursive function
        public void printUptoN(int n) {
            //condition for calling
            if (n > 1)
                printUptoN(n - 1);
            Console.Write(n + " ");
        }

        static void Main(string[] args) {
            // declare variables
            int n;

            // declare object
            Program obj = new Program();

            //take input of the number upto which 
            // natural numbers will be printed
            Console.Write("Enter the upper limit= ");
            n = Convert.ToInt32(Console.ReadLine());

            Console.Write("First " + n + " natural numbers are : ");
            // call the recursive function to print
            // the natural numbers
            obj.printUptoN(n);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the upper limit=4

First 4 natural numbers are : 1 2 3 4


Case 2:

Enter the upper limit=8

First 8 natural numbers are : 1 2 3 4 5 6 7 8