Pritam Karmakar Pritam Karmakar - 16 days ago 6
C# Question

How to reverse a number as an integer and not as a string?

I came across a question "How can one reverse a number as an integer and not as a string?" Could anyone please help me to find out the answer.

Answer

This should do it:

int n = 12345;
int left = n;
int rev = 0;
while(left>0)
{
   r = left % 10;   
   rev = rev * 10 + r;
   left = left / 10;  //left = Math.floor(left / 10); 
}

Console.WriteLine(rev);