Tuesday, 17 December 2013

Write a program which takes a integer and prints “YES” if its binary code (Excluding most significant bit) and its reverse are same;otherwise print “NO” for example 0101101101101

#include<stdio.h> int main() { int num,NOBITS; unsigned int firstbitmask=1,lastbitmask,x,y; NOBITS=8*sizeof(int)-1; lastbitmask=firstbitmask<<NOBITS; printf("Enter a number"); scanf("%d",&num); while(lastbitmask>firstbitmask) { x=num&lastbitmask; y=num&firstbitmask; if(((x==0)&&(y==0))||(x && Y)) { lastbitmask <<=1; firstbitmask >>=1; } else break; } if(lastbitmask==firstbitmask) printf("YES"); else printf("NO"); return 0; }

No comments:

Post a Comment