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

Answers were Sorted based on User's Feedback



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

Answer / rajkumar

#include<stdio.h>
void main()
{
int a,count=0;
while(a>0)
{
if(a%2==1)
count++;
a=a>>1;
}
printf("no of on bits =%d ",count);
}

Is This Answer Correct ?    15 Yes 1 No

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

Answer / vivek

int setbit=1; //Lets start checking from first bit
int numBitSet=0; //Number of bits set in the number

while(setbit>0)
{

if(number&setbit) //bit wise and
numBitSet++;

setbit=setbit<<1;
}

Is This Answer Correct ?    23 Yes 12 No

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

Answer / 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

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

Answer / krishna kanth

#include<stdio.h>
main()
{
int setbit=1;
int number=16;//for example
int numBitSet=0;
clrscr();

while(setbit<=number)//important and optimized condition
{

if(number&setbit)
numBitSet++;

setbit=setbit<<1;
}
printf("%d",numBitSet);
getch();
}

Is This Answer Correct ?    10 Yes 3 No

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

Answer / anilkumar

int n ; \\any number
for(count=0;n&=(n-1); count++);
printf("%d", count);

Is This Answer Correct ?    11 Yes 4 No

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

Answer / vaishu and hema

main()
{
int n,count=0,rem;
scanf("%d",&n);
while(n>0)
{
rem=n%2;
if(rem==1)
count++;
n=n/2;
}
printf("The number of on bits in the number is %d",count);
}

Is This Answer Correct ?    7 Yes 3 No

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

Answer / naveen

//plz let me know whether it is correct or not...bcoz am just beginner
#include<stdio.h>

void main()
{

int j=0,i=0xFFFFE,temp=1;
int count=0;


while(j<=(sizeof(int)*8-1))
{

if(i&temp)
count++;
temp=temp<<1;
j++;
}
printf("%d",count);

}

Is This Answer Correct ?    3 Yes 0 No

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

Answer / jaya j

unsigned int count1s(unsigned char x)
{
int p;
for(p=0;x!=0;x>>=1)
if(x&1)
p++;
return p;
}

Is This Answer Correct ?    2 Yes 0 No

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

Answer / madhu

#include <stdio.h>

main()
{
int number;
int setbit=1; //Lets start checking from first bit
int numBitSet=0; //Number of bits set in the number
printf("enter the number");
scanf("%d", &number);

while(setbit>0)
{

if(number&setbit) //bit wise and
numBitSet++;

setbit=setbit<<1;
}
printf("%d",numBitSet);
}
full program of the first ans...

Is This Answer Correct ?    2 Yes 1 No

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

Answer / ajay vikram

void main()
{
int number,a=0,b,count=0;
printf("Enter the number : ");
scanf("%d",&number);
b = (number/2);
while(b)
{
if((number>>a)& 1)
{
count++;
}
a++;
b--;
}
printf("Number of ON Bits in the number : %d\n",count);
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Interview Questions

What is switch in c?

0 Answers  


Can a local variable be volatile in c?

0 Answers  


Why Modern OS are interrupt driven?Give an example

3 Answers  


Explain the use of #pragma exit?

0 Answers  


Which of the following operators is incorrect and why? ( >=, <=, <>, ==)

0 Answers  






What is volatile c?

0 Answers  


What is structure in c language?

0 Answers  


On most computers additional memory that is accessed through an adapter of feature card along with a device driver program. a) user memory b) conventional memory c) expandedmemory d) area

0 Answers  


Can you pass an entire structure to functions?

0 Answers  


Design a program using an array that searches a number if it is found on the list of the given input numbers and locate its exact location in the list.

4 Answers  


At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E &#133;&#133;.. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with these names could be kept in that shelf as in this example: bottom of shelf ---> [AAAJKRDFDEWAAYFYYKK]-----Top of shelf. All these packets are to be loaded on cars. The cars are lined in order, so that the packet could be loaded on them. The cars are also named [A, B, C, D, E,&#133;&#133;&#133;&#133;.]. Each Car will load the packet with the same alphabet. So, for example, car &#145;A&#146; will load all the packets with name &#145;A&#146;. Each particular car will come at the loading point only once. The cars will come at the loading point in alphabetical order. So, car &#145;B&#146; will come and take all the packets with name &#145;B&#146; from the shelf, then car &#145;C&#146; will come. No matter how deep in the shelf any packet &#145;B&#146; is, all of the &#145;B&#146; packets will be displaced before the &#145;C&#146; car arrives. For that purpose, some additional shelves are provided. The packets which are after the packet B, are kept in those shelves. Any one of these shelves contains only packets, having the same name. For example, if any particular shelf is used and if a packet with name X is in it, then only the packets having names X will be kept in it. That shelf will look like [XXXXXXX]. If any shelf is used once, then it could be used again only if it is vacant. Packets from the initial shelf could be unloaded from top only. Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.

0 Answers   Infosys,


program to find error in linklist.(i.e find whether any node point wrongly to previous nodes instead of next node)

0 Answers   Huawei,


Categories