C Exercises: Check whether a number is a palindrome or not
38. Palindrome Number Check
Write a C program to check whether a number is a palindrome or not.
This program should prompt the user to input a number, then check if the number reads the same forwards and backwards. If the number is a palindrome, the program will display a message confirming this; otherwise, it will indicate that the number is not a palindrome.
Visual Presentation:

Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
void main(){
int num, r, sum = 0, t; // Declare variables for the original number, remainder, reversed number, and temporary variable.
printf("Input a number: "); // Prompt the user to input a number.
scanf("%d", &num); // Read the input from the user.
for(t = num; num != 0; num = num / 10){ // Loop to reverse the digits of the number.
r = num % 10; // Extract the last digit (remainder when divided by 10).
sum = sum * 10 + r; // Build the reversed number by adding the extracted digit.
}
if(t == sum) // Check if the original number and the reversed number are equal.
printf("%d is a palindrome number.\n", t); // Print a message if it is a palindrome.
else
printf("%d is not a palindrome number.\n", t); // Print a message if it is not a palindrome.
}
Output:
Input a number: 121 121 is a palindrome number.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C program to check if a number is a palindrome using recursion for digit reversal.
- Write a C program to determine if a number is a palindrome by comparing digits from both ends.
- Write a C program to verify a number's palindrome property without converting it to a string.
- Write a C program to check if a number is a palindrome and then compute the sum of its digits.
Go to:
PREV : Reverse a Given Number.
NEXT : Integers Divisible by 9 between 100 and 200.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.