C Program to Reverse a Number
C Program to Reverse a Number
A school number puzzle needs reverse a number for a nonnegative decimal value. Work on a copy so the original remains available for the final decision.
Build a reversed decimal value by multiplying the accumulated prefix by ten and appending the next extracted digit.
Before this exercise, review integer division and remainders and loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why reverse a number uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use 45, trace the digits and predict the reported result.
Reverse a Number: follow the data
Given data
A school number puzzle needs reverse a number for a nonnegative decimal value. Work on a copy so the original remains available for the final decision.
Rule
Build a reversed decimal value by multiplying the accumulated prefix by ten and appending the next extracted digit.
Observed result
Reversed: 21
Understand the operation
% 10 extracts the last decimal digit; division by ten removes it. reversed * 10 shifts the previously collected digits before the next one is appended.
This is a numeric value exercise. Leading zeros in a printed identifier belong to text and are not preserved by an integer representation.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
unsigned value = 120, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("Reversed: %u\n", reversed);
return 0;
}
Expected output
Reversed: 21
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
unsigned value = 120, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("Reversed: %u\n", reversed);
return 0;
}
Start with the original value 120 and a separate working copy.
The digit operations produce the reported result: Reversed: 21.
Follow the worked calculation
Start with the sample
Start with the original value 120 and a separate working copy.
Apply the rule
The digit operations produce the reported result: Reversed: 21.
Check the result
Reversed: 21
Reversed: 21
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
unsigned value = 120, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("Reversed: %u\n", reversed);
return 0;
}
Use 45, trace the digits and predict the reported result.
Worked answer
The changed input produces Reversed: 54. The original remains separate from the shrinking copy so the final comparison or label can still refer to it.
C17 · complete program
#include <stdio.h>
int main(void) {
unsigned value = 45, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("Reversed: %u\n", reversed);
return 0;
}
Expected output
Reversed: 54
Run in C compiler →Recognize the important boundaries
These fixtures stay below 10000. A general reversed value or square can overflow even when the input itself fits; validate the chosen domain or check each multiply/add.
Negative inputs need an explicit sign policy. Do not assume negating the minimum signed integer is representable.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Build a reversed decimal value by multiplying the accumulated prefix by ten and appending the next extracted digit.
Continue with separate stored values.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.