C program to check whether a number is palindrome or not
In this post, you will learn how to write a C program to check whether a number is a palindrome or not.
A Palindrome is a number, string, or sequence that remains the same when it is reversed. A palindrome sequence can be a combination of digits or numbers, but it looks the same in forward and backward. It is generally used where recreational mathematics is applied, like puzzles, games. To check whether a number or string is palindrome or not, simply reverse it and compare it with the original number or string. If both are same, then the number or string is a palindrome, otherwise not.
121, 1991, 1239321, MAAM, DAD, WOW
When we reverse the above numbers or strings, they will remain the same. So, all the above are palindrome.
C program to check palindrome using while loop
In the given C program, first we ask the user to enter a number. Then, we generate a new number by reversing the original number. Next, we have checked if the original number and reversed number are the same. The number is a palindrome.
// C program to check palindrome
#include <stdio.h>
int main()
{
int num, rev_num=0, remainder, temp;
printf("Please enter an integer: ");
scanf("%d", &num);
/* Generating new number by reversing
the original number */
temp=num;
while(temp!=0)
{
remainder=temp%10;
rev_num=rev_num*10+remainder;
temp/=10;
}
// check both original number and reverse number
if(rev_num==num)
printf("%d is a palindrome number.",num);
else
printf("%d is not a palindrome number.",num);
return 0;
}
Output 1 -
Please enter an integer: 45654
45654 is a palindrome number.
Output 2 -
Please enter an integer: 1223221
1223221 is a palindrome number.
C program to check palindrome using recursion
A recursion function is a function that is called itself. In the given example, we call the recursion function to check the palindrome. A recursion function continues until some condition is met to prevent it. That's why, we use the if else statement to break the infinite recursion. Here, we have used a user-defined recursion function is_palindrome() to check whether the given number is palindrome or not.
#include<stdio.h>
int is_palindrome(int num){
static int rev_num=0,rem;
if(num!=0){
rem = num%10;
rev_num = rev_num*10+rem;
is_palindrome(num/10);
}
return rev_num;
}
int main(){
int num, rev_num;
printf("Please enter a number : ");
scanf("%d",&num);
rev_num = is_palindrome(num);
if(num == rev_num)
printf("%d is a palindrome number.",num);
else
printf("%d is not a palindrome number.",num);
return 0;
}
Output of the above code:
Please enter a number : 4566654
4566654 is a palindrome number.
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative