66. Check if a number is palindrome Yes: Print number of set bits No: Print number of clear bits
#include<stdio.h>
void
main()
{
int temp,num,r,i,q,rev=0,set_bit=0;
printf("Check if a number is
palindrome\n Yes: Print number of set bits\n No: Print number of clear
bits\n");
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
for(;temp;temp=temp/10)
{
r=temp%10;
rev=rev*10+r;
}
for(i=0;i<=31;i++)
{
q =
(num>>i)&1;
set_bit = set_bit + q;
}
if(num==rev)
{
printf("PALINDROME\n");
printf("Number of set bits
: %d\n",set_bit);
}
else
{
printf("NOT
PALINDROME\n");
printf("Number of clear
bits : %d\n",32-set_bit);
}
}
Comments
Post a Comment