C# Program to Reverse a Number
Posted by Superadmin on August 15 2022 15:33:45

C# Program to Reverse a Number

 

This is a C# Program to get a number and display the number with its reverse.

Problem Description

This C# Program Gets a Number and Display the Number with its Reverse.

Problem Solution

Here we obtain a number from the user and display the digits in the reverse order.

Program/Source Code

Here is source code of the C# Program to Get a Number and Display the Number with its Reverse. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Get a Number and Display the Number with its Reverse
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, reverse = 0;
            Console.WriteLine("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            while (num != 0)
            {
                reverse = reverse * 10;
                reverse = reverse + num % 10;
                num = num / 10;
            }
            Console.WriteLine("Reverse of Entered Number is : "+reverse);
            Console.ReadLine();
 
        }
    }
}
Program Explanation

In this C# program, we are reading the number using ‘num’ variable. Multiply the value of ‘reverse’ variable by 10 and add this value of reverse variable with the modulus of the value of ‘reverse’ variable by 10. Compute the division of the value of ‘num’ variable by 10. Print the number with its reverse order.

 
Runtime Test Cases
 
Enter a Number : 123
Reverse of Entered Number : 321