C# Program to Print All Natural Numbers from 1 to N in Iterative Methods

First N natural numbers

C# program to print all the natural numbers from 1 to N can be written in both iterative and recursive methods. In the iterative method, the task can be performed by using for loop, while loop and do-while loop. Here, all the C# programs using different loops have been covered to print the natural numbers from 1 to N (1, 2, 3, ..., N).






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

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

using System;

namespace PrintNaturalNumber {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int n, i;

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

            // iterate from 1 to n and print the number
            Console.Write("First " + n + " natural numbers are : ");
            for (i = 1; i <= n; i++)
                Console.Write(i + " ");

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

Output


Case 1:

Enter the upper limit=10

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


Case 2:

Enter the upper limit=5

First 5 natural numbers are : 1 2 3 4 5

/************************************
        alphabetacoder.com
C# program to print all natual 
numbers from 1 to n  using while loop
**************************************/

using System;

namespace PrintNaturalNumber {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int n, i;

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

            // iterate from 1 to n and print the number
            Console.Write("First " + n + " natural numbers are : ");
            i = 1;
            while (i <= n) {
                Console.Write(i + " ");
                i = i + 1;
            }

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

Output


Case 1:

Enter the upper limit=10

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


Case 2:

Enter the upper limit=5

First 5 natural numbers are : 1 2 3 4 5

/**************************************
        alphabetacoder.com
C# program to print all natual numbers 
from 1 to n  using do - while loop
***************************************/

using System;

namespace PrintNaturalNumber {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int n, i;

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

            // iterate from 1 to n and print the number
            Console.Write("First " + n + " natural numbers are : ");
            i = 1;
            do {
                Console.Write(i + " ");
                i = i + 1;
            } while (i <= n);

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

Output


Case 1:

Enter the upper limit=10

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


Case 2:

Enter the upper limit=5

First 5 natural numbers are : 1 2 3 4 5