C# program to reverse a number


Here in this post we are going to learn how to reverse a number using C#.Net

What is reverse a number:

If user enters the number 12345, and if we reverse this number we will get 54321 as a reversed number.

So here 12345 is a number entered by user and 54321 is a reversed number.

What can we cover in this program:

1. we will ask user to enter a number on the console

2. we will read the number entered by the use and reverse that number

3. we will print the enter number and reversed number on the console

Reverse number program:

Code Snippet 1
using System;

namespace AlgorithamsAndLogics
{
    class ReverseNumber
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a number to reverse!");
            int number = Convert.ToInt32(Console.ReadLine());
            int tempnumber = number;
            int reverseNumber = 0;
            while (tempnumber > 0)
            {
                var remainder = tempnumber % 10;
                reverseNumber = reverseNumber * 10 + remainder;
                tempnumber = tempnumber / 10;
            }
            Console.WriteLine($"Actual number: {number}");
            Console.WriteLine($"Reversed number: {reverseNumber}");
        }
    }
}

Output:

image

Happy coding..!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s