C Program to Check a Palindrome Number
C Program to Check a Palindrome Number
A school number puzzle needs palindrome number for a nonnegative decimal value. Work on a copy so the original remains available for the final decision.
A nonnegative decimal integer is a palindrome when reversing its digits produces the same numeric value.
Before this exercise, review integer division and remainders and loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why palindrome number uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use 120, trace the digits and predict the reported result.
Palindrome Number: follow the data
Given data
A school number puzzle needs palindrome number for a nonnegative decimal value. Work on a copy so the original remains available for the final decision.
Rule
A nonnegative decimal integer is a palindrome when reversing its digits produces the same numeric value.
Observed result
121: palindrome
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 = 121, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("%u: %s\n", value, value == reversed ? "palindrome" : "not palindrome");
return 0;
}
Expected output
121: palindrome
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
unsigned value = 121, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("%u: %s\n", value, value == reversed ? "palindrome" : "not palindrome");
return 0;
}
Start with the original value 121 and a separate working copy.
The digit operations produce the reported result: 121: palindrome.
Follow the worked calculation
Start with the sample
Start with the original value 121 and a separate working copy.
Apply the rule
The digit operations produce the reported result: 121: palindrome.
Check the result
121: palindrome
121: palindrome
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
unsigned value = 121, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("%u: %s\n", value, value == reversed ? "palindrome" : "not palindrome");
return 0;
}
Use 120, trace the digits and predict the reported result.
Worked answer
The changed input produces 120: not palindrome. 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 = 120, remaining = value, reversed = 0;
do { reversed = reversed * 10 + remaining % 10; remaining /= 10; } while (remaining != 0);
printf("%u: %s\n", value, value == reversed ? "palindrome" : "not palindrome");
return 0;
}
Expected output
120: not palindrome
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
A nonnegative decimal integer is a palindrome when reversing its digits produces the same numeric value.
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.