Here is a C program to check whether a number is palindrome or not. A number is palindrome, if number remains same after reversing it's digits.
For Example
432234 is palindrome number, but 54321 is not a palindrome number.
This program first takes a number as input form user. Then, to check whether number is palindrome or not, we reverse the digits of number and then compare it with original number. If original number and it's reverse are same then number is palindrome otherwise not a palindrome.
- Take a number as input from user and store it in an integer variable(Let's call it inputNumber).
- Reverse the digits of inputNumber, and store it in another integer variable(Let's call it reverseNumber).
- Compare inputNumber and reverseNumber.
- If both are equal then inputNumber is palindrome otherwise not a palindrome.
C program to check a number is palindrome or not
This program first takes an integer as input from user and stores it in variable 'inputNumber'. Then it copies the value of input number to variable 'temp'. Now using a while loop it reverses the digits of temp(click here to know how to reverse a number Reverse a number). We then compare the values of inputNumber and temp, If they are equal then inputNumber is palindrome otherwise not a palindrome.
#include <stdio.h> int main(){ int inputNumber, reverseNumber = 0, rightDigit, temp; printf("Enter a number \n"); scanf("%d", &inputNumber); temp = inputNumber; while(temp != 0){ rightDigit = temp % 10; reverseNumber = (reverseNumber * 10) + rightDigit; temp = temp/10; } if(reverseNumber == inputNumber){ printf("%d is Palindrome\n", inputNumber); } else { printf("%d is not a Palindrome\n", inputNumber); } return 0; }Program Output
Enter a number : 12321 12321 is Palindrome
Enter a number : 12345 12345 is not a Palindrome
Related Topics