Write a C# Program to Print All Natural Numbers in Reverse Order

Do you want to know how to print all natural numbers in reverse order in C#? In this C# tutorial, I will explain how to write a C# program to print all natural numbers in reverse order.

To write a C# program that prints natural numbers in reverse order, you would use a for loop that starts from the specified upper limit and decrements down to 1. Within the loop, print each number, ensuring the loop continues as long as the current number is greater than or equal to 1.

C# Program to Print All Natural Numbers in Reverse Order

Natural numbers are a sequence of numbers starting from 1 and increasing by 1 each time (1, 2, 3, 4, …). In our task, we need to print these numbers in reverse order. For example, if our upper limit is 10, the output should be 10, 9, 8, …, down to 1.

Here, we will write a C# console application to print all natural numbers in reverse order.

Here is the complete code:

using System;

class ReverseNaturalNumbers
{
    static void Main()
    {
        Console.Write("Enter the upper limit: ");
        int upperLimit = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Natural numbers in reverse order:");
        for(int i = upperLimit; i >= 1; i--)
        {
            Console.Write(i + " ");
        }
    }
}
  • We start by asking the user to input the upper limit for the natural numbers.
  • The for loop starts from the upper limit and decrements the value of i in each iteration.
  • The loop continues to execute as long as i is greater than or equal to 1.
  • In each iteration, the current value of i is printed, followed by a space.

Once you run the code, it will ask you to enter the number (upper limit). Then, it prints all the natural numbers like in the below screenshot.

write c# program to print all natural numbers in reverse order

Conclusion

In this C# tutorial, I have explained how to write a C# program to print all natural numbers in reverse order. We wrote a C# console application and run the code to get the output properly.

You may also like: