Reverse a number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Reverse
{
class Program
{
static void Main(string[] args)
{
/*Accept the number to be reversed and store in a temp
variable. Then modulus of the number
* by 10 gives the last digit. Add
that to a string. Also divide the number by 10 , so that
* the last number is left out */
Console.WriteLine("Enter
the number to be reversed : ");
int number = Convert.ToInt32(Console.ReadLine());
int tempnumber = number;
string lastdigit = "";
string final = "";
while ( tempnumber >= 1)
{
lastdigit = (tempnumber % 10).ToString();
tempnumber = tempnumber / 10;
final = final + lastdigit;
}
Console.WriteLine(final);
Console.ReadLine();
}
}
}
Output :
No comments:
Post a Comment