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 ofi
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.
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:
- How to Write a Program to Find a Leap Year in C#
- Write A C# Program To Print The Multiplication Table Of A Number
- Write a C# Program to Print the Factorial of a Number
- Write A C# Program To Reverse The Words Of A Sentence
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…