Program to check whether the given number is palindrome or not | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Program to check whether the given number is palindrome or not

27th Oct 2016, 3:52 PM
Sankalp
3 Answers
+ 5
#include<iostream> using namespace std; int main(void) { int no, n, rev=0, dgt; //initially, rev is zero. cout<<"Enter a Number - ";cin>>no; n=no; //so that original no can be printed later. while(n!=0) { dgt=n%10; //to get last digit of n rev=rev*10+dgt; //to add digit to reverse and multilply by 10 to create new vacant space in rev n/=10; //since n is int all decimals will go away and next digit can be obtained. } cout<<"Number - "<<no<<endl; cout<<"Reverse - "<<rev<<endl; //now the check below. if(no==rev) cout<<"Palindrome"; else cout<<"Not a Palindrome"; }
27th Oct 2016, 4:08 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
That should do it...
27th Oct 2016, 4:09 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
#include<iostream> using namespace std; int main() { int num,n,r,rev; rev = 0; cout<<"Enter the number: "; cin>>num; n = num; while(n>0) { r = n%10; rev = (rev*10)+r; n = n/10; } if(num==rev) cout<<"Number "<<num<<" is palindrome"; else cout<<"Number "<<num<<" is not palindrome"; }
27th Oct 2016, 6:48 PM
Shad Ahmad Zaidi
Shad Ahmad Zaidi - avatar