Problem: Write a C program to check whether a number is a Palindrome or not.
Palindrome number is that number which when reversed, is equal to the original number.
Examples: 111, 121, 8008 etc.
Steps to Check Palindrome in C:
- Input a number.
- Run while loop with condition
(number != 0)
- Divide the number by 10
- Store the remainder into another variable
reverse
as statedreverse = reverse *10 + remainder
- Update the number as
number = number/10
- After the loop ends, check if
reverse == original_number
- If yes, then the number is palindrome else not.
Here is the implementation of the steps in C.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int original_number, number, remainder, reverse =0;
printf("Enter a Number \n");
scanf("%d",&original_number);
//By copying original_number we are keeping it safe from changes
number = original_number ;
while(number != 0){
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
}
if(original_number == reverse)
printf("Palindrome \n");
else
printf("Not Palindrome \n");
return 0;
}
Output
When the input number is 456.
When the input number is 121.
In this tutorial, we learned what is palindrome number and how to check palindrome number in C programming language.