Program to reverse number in C#
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.
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.
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);
}
}
Enter a number: 234
Reversed Number: 432
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.