Check Palindrome: Binary & Decimal

Please support our C++ advertiser: Intel Parallel Studio Home
Rhohitman Rhohitman is offline Offline Jun 24th, 2009, 10:42 pm |
0
Check Palindrome: Binary & Decimal
Quick reply to this message  
C++ Syntax
  1. # include <iostream>
  2. # include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. void binPaldrome(unsigned int);
  7. void showbits(unsigned int);
  8.  
  9. int main()
  10. {
  11. unsigned int no;
  12. for(;;)
  13. {
  14. printf("Check Palindrome ");
  15. system("time /t");
  16.  
  17. switch(getch())
  18. {
  19. case 27: return 0;
  20. case 13:
  21. cout<<"Enter the no: ";
  22. cin>>no;
  23. binPaldrome(no);
  24. }
  25. }
  26. return 0;
  27. }
  28.  
  29. void binPaldrome(unsigned int n)
  30. {
  31. unsigned int copy=n, rev=0;
  32. while(copy>0)
  33. {
  34. rev=(rev*10)+copy%10;
  35. copy/=10;
  36. }
  37.  
  38. printf("%d ",n); showbits(n);
  39. if(rev==n) printf("\nDecimal Palindrome %c", 251);
  40. else printf("\nDecimal Palindrome X");
  41.  
  42. int i, j, andmask, c1, c2, flag=0;
  43. for(i=15; i>=0; i--)
  44. {
  45. andmask=1<<i;
  46. c1 = n & andmask;
  47. if(c1!=0) break;
  48. }
  49.  
  50. for(j=0;;j++)
  51. {
  52. andmask=1<<j;
  53. c2 = n & andmask;
  54. if((i-2)==j) break;
  55. if(c1==c2 || (c1!=0 && c2!=0))
  56. {
  57. i--;
  58. andmask=1<<i;
  59. c1 = n & andmask;
  60. }
  61. else
  62. {
  63. flag=1;
  64. break;
  65. }
  66. }
  67.  
  68. if(flag==0) printf("\nBinary Palindrome %c", 251);
  69. else printf("\nBinary Palindrome X");
  70. printf("\n");
  71. }
  72.  
  73. void showbits(unsigned int n)
  74. {
  75. int i, k, andmask;
  76. for(i=15; i>=0; i--)
  77. {
  78. andmask=1<<i;
  79. k = n & andmask;
  80. k==0? printf("0") : printf("1");
  81. }
  82. }

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC