PUBLISHED ON: APRIL 29, 2022
C++ Program To Check Number Is Palindrome Or Not
In this tutorial, we will learn how to check whether any number is palindrome or not.
Program To Check Number Is palindrome Or Not In C++ Language
A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers.
Palindrome number algorithm
- Get the number from user
- Hold the number in temporary variable
- Reverse the number
- Compare the temporary number with reversed number
- If both numbers are same, print palindrome number
- Else print not palindrome number
Let's see the palindrome program in C++. In this program, we will get an input from the user and check whether number is palindrome or not.
#include<bits/stdc++.h>
using namespace std;
void check_palindrome(int a){
int temp=a;
int res=0;
for(int i=a;i>0;i=i/10){
res+=res*10+i%10;
}
if(temp==res){
cout<<a<<" is an palindrome number";
}
else{
cout<<a<<" is not an palindrome number";
}
}
int main(){
int num;
cout<<"Enter the number that you want to check:-";
cin>>num;
check_palindrome(num);
return 0;
}
Enter the number that you want to check:-16464
16464 is not an palindrome number
Conclusion
We have learned what is a palindrome number and how can we write a C++ code for checking whether any given number is palindrome or not.