re: Daily Coding Puzzles - Oct 29th - Nov 2nd VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Wednesday Write a program to reverse the digits of a positive integer but without converting it to a string.
 

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
code of conduct - report abuse