DEV Community

Discussion on: Daily Coding Puzzles - Oct 29th - Nov 2nd

Collapse
 
tux0r profile image
tux0r • Edited

As nothing in the question says that the result must still be an integer, why not make it an array instead? That would also elegantly solve the problem that an input that ends with a "0" would be chopped... or, even better, don't use any return value. After all, you did not ask for one.

#include <stdio.h>

void reverse_int(int in) {
    while (in > 0) {
        printf("%d", in % 10); /* modulo 10 ... */
        in /= 10; /* ... and move one digit. */
    }
}

/* PoC: */
int main(void) {
    reverse_int(1234567890);

    return 0;
}

Output:

0987654321