what is a palindromic number?
A palindromic number is a number which remains same when its digits are reversed. Example 121,16461 etc.
Input=number reverse_number=0; temp=number; while loop number > 0 (a)multiply reverse_number by 10 and add remainder of number divide by 10 to reverse_number reverse_number= reverse_number*10+number%10; (b)divide number by 10 number=number/10; if(rev_num==temp) print number is palindrome else print number is not palindrome exit;
In the above program, we store the number in temp then reverse the number and at last, we compare the reversed number with temp.
program:-
#include<iostream>
using namespace std;
int main()
{
int num,temp,rev_num=0;
cout<<"Enter the number:";
cin>>num;
temp=num;
while(num>0)
{
rev_num=rev_num*10+num%10;
num=num/10;
}
if(rev_num==temp)
cout<<"number is palindrome";
else
cout<<"number is not palindrome";
return 0;
}
output:-
Enter the number:3456
number is not palindrome
Enter the number:121
number is palindrome
Program to print palindrome number between 1 to 50:-
#include<iostream>
using namespace std;
int main()
{
int n,num,sum,temp;
cout<<"palindrome numbers are:";
for(num=1;num<=50;num++)
{
temp=num;
sum=0;
while(temp)
{
n=temp%10;
temp=temp/10;
sum=sum*10+n;
}
if(num==sum)
cout<<"\t"<<num;
}
return 0;
}
Output:-
palindrome numbers are: 1 2 3 4 5 6 7
8 9 11 22 33 44
0 comments:
Post a Comment