C++ Program to Check Palindrome Number

Last updated on:

In this tutorial, we will learn about Palindrome numbers in C++ i.e. how to write a program to check whether a number is a Palindrome or not using C++.

Quick Info:💡

↪ An integer is considered as Palindrome if the reverse of the number is equal to its original number.

↪ For example, 121, 343, 1221, etc are the palindrome numbers.

↪ Here’s the following steps to check palindrome:

Code to check palindrome number can be performed in two ways:

Palindrome Number in C++

Here’s a direct method to check a palindrome number.

Simply, the user will be asked to enter an integer then, the number is computed according to code provided and then displayed to a user.

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
int num,rev=0,rem,temp;
cout<<"enter a number:=";
cin>>num;
temp=num;
while(temp>0)
{
rem=temp%10;
rev=rev*10+rem;
temp/=10;
}
cout<<"Number was:"<<num<<endl;
cout<<"Reverse is:"<<rev<<endl;
if(num==rev)
cout<<"So, number is Palindrome";
else
cout<<"So, number is not Palindrome";
getch();
}
//Output:
Enter a number 343
Number was: 343
Reverse is: 343
So, number is Palindrome
Palindrome_simple

Working:

Flow of a program:

palindrome_working

Palindrome Number Using Function

Functions are used to perform certain actions, and they are important for reusing code: Define the code once and use it many times.

And, this is a another method to check whether it is palindrome or not i.e. by function

Note: Always remember that execution starts from the main body only.

Example:

#include<iostream.h>
#include<conio.h>
int palindrome(int num)
{
int rev=0,rem,temp;
temp=num;
while(temp>0)
{
rem=temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(rev==num)
cout<<"Number is Palindrome";
else
cout<<"Number is not Palindrome";
return 0;}
int main(){
int n;
cout<<"enter any number:=";
cin>>n;
palindrome(n);
getch();
return 0;
}
//Output:
enter any number:=345
Number is not Palindrome
Palindrome_func

Working:

Flow of a program:

Palindrome_work2_func

Well, I hope it really helped you to know C++ better.

Attention reader⚠ Don’t stop learning now.

Just stay with us to get more such exciting codes to explore more in C++.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *