+ 3
How to reverse a number?
3 ответов
0
Here's the algorithm:
1)Get a number to reverse as an input.
2)Declare two integer variables, varA and varB;
varA for the reversed number(this has to be initialized with a value of 0),
varB for the original number.
3)Now, until varB reaches 0, repeat this loop.
3-1)Multiply varA by 10.
3-2)Add thr remainder of (varB/10) to varA.
3-3) Divide varB by 10.
4)If the loop is finished and it's correctly coded, you will have a reversed number, and the value is stored via varA.
0
void Main();
{
Console.Write("Enter the number to be reversed");
int n=Convert.ToInt32(Console.ReadLine());
int a,rev=0;
while(n>0)
{
a=n%10;
rev=rev*10+a;
n=n/10;
}
Console.WriteLine(rev);
}
0
void rev(int num)
{
int rev=0;
while(num)
{
rev = (rev*10)+(num%10);
num=num/10;
}
//Now Print The Number.
}