-
C++ (
http://www.daniweb.com/forums/forum8.html)
| Rhohitman | Jun 24th, 2009 10:42 pm | |
| 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");
}
}
| All times are GMT -4. The time now is 5:25 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC