A number is called as the palindrome number if the number is equal after reverse the number. For Example : 58485 is a palindrome number.
// Write a c program to check the given number is palindrome or not.
#include<stdio.h>
int reverseNumber(int n){
int reverse = 0;
while(n > 0){
int last_digit = n % 10;
reverse = reverse * 10 + last_digit;
n = n / 10;
}
return reverse;
}
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
if(n == reverseNumber(n)){
printf("Palindrome");
}else{
printf("Not a palindrome");
}
return 0;
}
#include<stdio.h>
It is include the standard library for using the inbuild function of the input/output function.
// Function to reverse the number.
int reverseNumber(int n){
int reverse = 0;
while(n > 0){
int last_digit = n % 10;
reverse = reverse * 10 + last_digit;
n = n / 10;
}
return reverse;
}
Above the reverse function is calculate the reverse of a given number, using the while loop that execute the loop body n > 0, after calculated the reverse then return the value of reverse number. For more info Read now.
// main function
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
if(n == reverseNumber(n)){
printf("Palindrome");
}else{
printf("Not a palindrome");
}
return 0;
}
In the main function, declared the variable n as integer. Read the value from the user and store in the n variable. Check the given number and reverse number is both are equal then output is “palindrome” otherwise number is “not a palindrome” using the printf() function and check the condition using if-else statement.
// pseudocode of palindrome number
1. INCLUDE stdio.h
2. FUNCTION reverseNumber(n)
1. SET int reverse = 0;
2. while n > 0 then
1. int last_digit = n % 10
2. reverse = reverse * 10 + last_digit
3. n = n / 10
3. RETURN reverse
3. FUNCTION main()
1. DECLARE n AS INTEGER
2. PRINT "Enter the number : "
3. READ n FROM USER
4. IF n == reverseNumber(n)
PRINT "Palindrome"
5. ELSE
PRINT "Not a palindrome"
6. RETURN 0;
Comments
Oops!