Search code examples
cunsigned-integer

reverse number for max unsigned int


I try to write a program for reverse the user input number.

The user input range is from 0 < a < 4294967295, here is EDITED code.

unsigned long int reverseNumber(unsigned long int num)
{
    unsigned long int rev = 0;

    while (num > 0)
    {
        rev = rev *10 + (num%10);
        num = num/10;
    }

    return rev;
}

The problem is when I input 4294967295, it will output 1632727628. Why? I have no idea why it happened. How can I reverse the 4294967295.

I had changed it to unsigned long int, printf by using %lu, but still output 1632727628. Why?


Solution

  • The reverse of 4294967295 is 5927694924, which is greater than 4294967295, which is the greatest integer which can be stored on 32 bit.