Written: August 15, 2026
Reversing digits trains the modulo pattern: peel the last digit, append it to a growing result, shrink the original.
This walkthrough keeps the logic small and readable so you can type it, run it, and then change one input at a time to see what happens.
What you will build
While n > 0: rev = rev10 + n%10; n /= 10.
- Read a non-negative integer n
- Initialize rev = 0
- Loop: take last digit, shift rev left by one decimal place, add digit
- Print rev
Working C example
include <stdio.h>
int main(void) {
int n, rev = 0;
printf("Enter a non-negative integer: ");
if (scanf("%d", &n) != 1 || n < 0) return 1;
while (n > 0) {
rev = rev 10 + n % 10;
n /= 10;
}
printf("Reversed = %d\n", rev);
return 0;
}
How the logic works
Read the program top to bottom: includes and main first, then the statements that change variables, then the print that proves the result.
If your output looks wrong, print the variables before and after the critical lines. That single habit catches most beginner bugs faster than rewriting the whole file.
Trailing zeros in the input disappear in the integer reverse (120 → 21). Mention that if your lab expects a string reverse instead.
Keep learning
If this walkthrough on C Program to Reverse a Number helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.
Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.