In this Blog, We will learn How to reverse a no. using C#.
Preview:
using System;
namespace ReverseNo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a No. to reverse");
int Number = int.Parse(Console.ReadLine());
int Reverse = 0;
while(Number>0)
{
int remainder = Number % 10;
Reverse = (Reverse * 10) + remainder;
Number = Number / 10;
}
Console.WriteLine("Reverse No. is {0}",Reverse);
Console.ReadLine();
}
}
}
|
[Download Source Code via Google Drive]
Watch Video:
I came up with the same algorithm. The only thing is, if you enter an integer that begins with one or more zeros, the loop will not include any of those zeros, it will terminate. So, is there a way to get it to include zeros at the beginning without using a string?
ReplyDelete