data-structures · beginner · ~15 min

Reverse a number

Reverse a number's digits recursively.

Challenge

Implement:

int reverse_number(int n);

Return n (n >= 0) with its decimal digits reversed, computed recursively. E.g. 1230 -> 321.

Input format

n >= 0.

Output format

Digit-reversed value.

Constraints

Trailing zeros of the input disappear (1230 -> 321).

Starter code

#include <stddef.h>
/* Reverse the decimal digits of n (n>=0), computed recursively. e.g. 1230 -> 321. */
int reverse_number(int n){ (void)n; return 0; }

Common mistakes

Trying to reverse without an accumulator argument.

Edge cases to handle

0 reverses to 0.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.