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 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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.