Home / Programs / Program to reverse number in C#
Programming Example

Program to reverse number in C#

👁 366 Views
💻 Practical Program
📘 Step by Step Learning

We can reverse a number in C# using loop and arithmetic operators. In this program, we are getting number as input from the user and reversing that number.

Let's see a simple C# example to reverse a given number.

Program Code

using System;  
  public class ReverseExample  
   {  
     public static void Main(string[] args)  
      {  
       int  n, reverse=0, rem;           
       Console.Write("Enter a number: ");      
       n= int.Parse(Console.ReadLine());     
       while(n!=0)      
       {      
        rem=n%10;        
        reverse=reverse*10+rem;      
        n/=10;      
       }      
       Console.Write("Reversed Number: "+reverse);       
    }  
  }  

Output

Enter a number: 234  
Reversed Number: 432

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.