Question d’entretien chez Bloomberg

Reverse an integer using C.

Réponses aux questions d'entretien

Utilisateur anonyme

3 oct. 2011

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

Utilisateur anonyme

3 oct. 2011

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

Utilisateur anonyme

17 janv. 2013

num = 0 for (i=0; i

Utilisateur anonyme

17 janv. 2013

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

Utilisateur anonyme

17 janv. 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.