Articles

# In this chapter of C++ program tutorial out task is:

• write a c++ program to check whether a number is palindrome or not using function

### C++ program to check Palindrome number

Below is the c++ program to check whether a number is palindrome or not using if statement and loop.

``````/*  Program Name : To check whether a number is Palindrome or not
Program Author : Sayantan Bose
*/

#include <iostream>
#include <math.h>
using namespace std;

//This function returns number we get by reversing the inputed number.
int palindrome(int num)
{
int r=0,sum=0,i,c;
string n = to_string(num);
c=n.size();
c=(pow(10,c))/10;
while(num!=0)
{
r=num%10;
sum=sum+r*c;
num=num/10;
c=c/10;

}
return sum;
}

// This is the main function
int main() {

int num,sum=0;
cout<<"Enter any number to check Palindrome:\n";
cin>>num;
int new_num;
new_num=num;
sum=palindrome(num);
if(sum==new_num)
cout<<num<<" is a Palindrome number";
else
cout<<num<< " is not a Palindrome number";

return 0;
}
``````

OUTPUT

Enter the number to check Palindrome:
121
121 is an Palindrome number
Or
Enter the number to check Palindrome:
53
53 is not an Palindrome number

EXPLANATION :
In the above c++ program to check whether a number is palindrome or not using function:
• A number is said to be Palindrome if and only if the reverse of the number is equal to the number itself.
• For example: 121 is a Palindrome number. Because 121 when reversed gives 121  which is the number itself.
• In the method palindrome, we are calculating the length of the number by converting it into a string. The number is reversed using the above logic and the reversed number is then returned to the main method.
• In the main method the reversed number is then compared with the original number provided as input.
• If both of them turn out to be equal the number is a Palindrome number else it isn't.
You can see the above code execution and output in codeblocks IDE: CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com 