Bloomberg L.P.

  www.bloomberg.com
  www.bloomberg.com

Interview Question

Engineer Interview New York, NY

How would you reverse the digits of an integer? (say from

  123456 to 654321)
Tags:
technical
Answer

Interview Answer

1 Answer

0

int reverseInteger( int nOriginal )
{
    int nResult = 0;
    int nRemainder = nOriginal % 10;

    while ( nRemainder != 0 )
    {
        nResult += nRemainder;
        nOriginal /= 10;
        nRemainder = nOriginal % 10;

        if ( nRemainder != 0 )
        {
            nResult *= 10;
        }
    }

    return nResult;
}

Stefan on Mar 13, 2010

Add Answers or Comments

To comment on this, Sign In or Sign Up.