Sollicitatievraag bij Bloomberg

Reverse an integer using C.

Antwoorden op sollicitatievragen

Anoniem

3 okt 2011

in C: int reverseint(int x) { int y, k=1; while ( x > 10 ) { y = k * ( x % 10 ); k *= 10; x /= 10; } return y; }

Anoniem

3 okt 2011

whoops! "int y" should be "int y=0" and "y = k * ( x%10 )" should be "y += k * (x%10)"

Anoniem

17 jan 2013

num = 0 for (i=0; i

Anoniem

17 jan 2013

Sorry the for loop goes from last to 0. And we decrement "i"

Anoniem

17 jan 2013

A simple code example. Here the number to be reversed is 621. #include int main() { int y=0, k=1, x=621; int a; while ( x > 1 ) { a = ( x % 10 ); y = y *10 + a; x /= 10; } printf ("%d", y); return 0; } And the output will be 126.