Check Palindrome: Binary & Decimal
Check Palindrome: Binary & Decimal
# include <iostream>
# include <conio.h>
using namespace std;
void binPaldrome(unsigned int);
void showbits(unsigned int);
int main()
{
unsigned int no;
for(;;)
{
printf("Check Palindrome ");
system("time /t");
switch(getch())
{
case 27: return 0;
case 13:
cout<<"Enter the no: ";
cin>>no;
binPaldrome(no);
}
}
return 0;
}
void binPaldrome(unsigned int n)
{
unsigned int copy=n, rev=0;
while(copy>0)
{
rev=(rev*10)+copy%10;
copy/=10;
}
printf("%d ",n); showbits(n);
if(rev==n) printf("\nDecimal Palindrome %c", 251);
else printf("\nDecimal Palindrome X");
int i, j, andmask, c1, c2, flag=0;
for(i=15; i>=0; i--)
{
andmask=1<<i;
c1 = n & andmask;
if(c1!=0) break;
}
for(j=0;;j++)
{
andmask=1<<j;
c2 = n & andmask;
if((i-2)==j) break;
if(c1==c2 || (c1!=0 && c2!=0))
{
i--;
andmask=1<<i;
c1 = n & andmask;
}
else
{
flag=1;
break;
}
}
if(flag==0) printf("\nBinary Palindrome %c", 251);
else printf("\nBinary Palindrome X");
printf("\n");
}
void showbits(unsigned int n)
{
int i, k, andmask;
for(i=15; i>=0; i--)
{
andmask=1<<i;
k = n & andmask;
k==0? printf("0") : printf("1");
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.