write a program to find out number of on bits in a number?

Answer Posted / venkat reddy

main()
{
int n,counter;
printf("enter a number");
scanf("%d",&n);// let n=5
while(n>0)
{
counter++;
n=n&(n-1);
}
printf("%d",counter);
getch();
}

Is This Answer Correct ?    11 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

the factorial of non-negative integer n is written n! and is defined as follows: n!=n*(n-1)*(n-2)........1(for values of n greater than or equal to 1 and n!=1(for n=0) Perform the following 1.write a c program that reads a non-negative integer and computes and prints its factorial. 2. write a C program that estimates the value of the mathematical constant e by using the formula: e=1+1/!+1/2!+1/3!+.... 3. write a c program the computes the value ex by using the formula ex=1+x/1!+xsquare/2!+xcube/3!+....

22237


How are structure passing and returning implemented?

593


What does main () mean in c?

616


What does the c in ctime mean?

571


What is c language used for?

560






Explain what is the difference between a free-standing and a hosted environment?

636


What is the method to save data in stack data structure type?

609


Why is c used in embedded systems?

614


Write a function stroverlap that takes (at least) two strings, and concatenates them, but does not duplicate any overlap. You only need to worry about overlaps between the end of the first string and the beginning of the second string. Examples: batman, manonthemoon = batmanonthemoon batmmamaman, mamamanonthemoon = batmmamamanonthemoon bat, man = batman batman, batman = batman batman, menonthemoon = batmanmenonthemoon

1741


What is the size of structure in c?

703


Why is c called a mid-level programming language?

729


C program execution always begins with a) #include b) comment (/*-------*/) c) main() d) declaration instructions

613


is it possible to create your own header files?

642


What is a structural principle?

641


a character or group of characters that defines a register,or a part of storage a) memory b) byte c) address d) linear list

633