C++ program to Check Number is Palindrome or not


Palindrome number is a number that remains the same when its digits are reversed. Like 16461, for example: we take 121 and reverse it, after revers it is same as original.

Steps to write program

  • Get the value from user.
  • Reverse it.
  • Compare it with the number entered by the user.
  • If both are same then print palindrome
  • Else print not a palindrome.

Palindrome Number Program in C++ using while loop

#include<iostream.h>
#include<conio.h>

void main()
{
int a,no,b,temp=0;
clrscr();
cout<<"Enter any num: ";
cin>>no;
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
getch();
}

Output

Enter any num: 143
Not Palindrome

Output

Enter any num: 141
Palindrome

Palindrome Number Program in C++ using for loop

#include<iostream.h>
#include<conio.h>

void main()
{
int a,no,b,temp=0;
clrscr();
cout<<"Enter any num: ";
cin>>no;
b=no;
for(;no>0;)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
cout<<"Palindrome";
}
else
{
cout<<"not Palindrome";
}
getch();
}

Output

Enter any num: 121
Palindrome

Output

Enter any num: 643
Not Palindrome

0 comments: