English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn how to check if the number entered by the user is a palindrome.
To understand this example, you should know the followingC programmingTopic:
If the reverse of the number is equal to the original number, then the integer is a palindrome.
#include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; //Reverse the integer and store it in reversedN. while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; {} //If originalN and reversedN are equal, then the number is a palindrome. if (originalN == reversedN) printf("%d is a palindrome.", originalN); else printf("%d is not a palindrome.", originalN); return 0; {}
Output the result
Enter an integer: 1001 1001 It is a palindrome.
Here, the user is asked to enter an integer. The number is stored in the variable n.
Then we assign this number to another variable orignalN. Then find the reverse of n and store it in reversedN.
If originalN equals reversedN, then the number entered by the user is a palindrome,