Reverse of a number
<p>How do we reverse a number with leading zeroes in number ?
  For ex: If input is 004, 
          output should be 400.</p>

<p>I wrote below program but it works only when no leading zeroes in input.</p>

<pre><code>int num;
cout&lt;&lt;"Enter number "&lt;&lt;endl;
cin&gt;&gt;num;

int rev = 0;
int reminder;
while(num != 0)
{
    reminder = num % 10;
    rev = rev * 10 + reminder;
    num = num / 10;
}
cout&lt;&lt;"Reverse = "&lt;&lt;rev&lt;&lt;endl;
</code></pre>

<p>Is there any way to input a number with leading zeroes ? Even then, Above logic doesn't work for such numbers.</p>

<p>Any simple solution ? It is doable by taking input as string and processing it. But that doesn't look nice.</p>

<p>*<strong><em>EDIT: If length of number is known, it looks to be possible to reverse a number with leading zeroes.</strong> (Without using string)</em>*</p>

<p>I shall post the code as soon as it works.</p>

<p>EDIT 2: I tried to put back characters to cin stream and then read and calculate the reverse. It is working for 2 digit numbers.</p>

<p>But if length is known, its far easier to find reverse. All i need to do is, multiply by 10 for required number of times.
So i think, i would go with string approach.
Hoping that interviewer would be happy :)</p>
