i have made the following program but i cant find what is the error...
the output is not coming correct...

for example
Enter A Binary Number 110011

Number of ones are 0
Number of zeroes are 1

void main()
{
  clrscr();
  int r, ones=0, zeroes=0;
  long int n;
  printf("Enter A Binary Number ");
  scanf("%d", n);
  while(n!=0)
  {
    r=n%10;
    if(r==1)
      ones++;
    if(r==0)
      zeroes++;
    n=n/10;
  }
  printf("\nNumber of ones are %d", ones);
  printf("\nNumber of zeroes are %d", zeroes);
  getch();
}

Recommended Answers

All 8 Replies

Read up on scanf and try to understand what it means to catch an argument.

I think if you change your scanf() line to:
scanf("%d", &n);
it will work for you.

okay did that...but still its not working...

void main()
{
  clrscr();
  int r, ones=0, zeroes=0;
  long int n;
  printf("Enter A Binary Number ");
  scanf("%d", &n);
  while(n!=0)
  {
    r=n%10;
    if(r==1)
      ones++;
    if(r==0)
      zeroes++;
    n=n/10;
  }
  printf("\nNumber of ones are %d", ones);
  printf("\nNumber of zeroes are %d", zeroes);
  getch();
}

In your first post you did good -- mentioned what you wanted and what the program actually did. This time you left it up to us to figure out what happens. always tell us
1) what you did,
2) what the program did, and
3) what you expected instead

/* void is not good*/ int main()
{
   /* clrscr(); remove this */
  int r, ones=0, zeroes=0;
  long int n;
  printf("Enter A Binary Number ");
  fflush( stdout ); /* use when you are not terminating a printf with a \n */
  scanf("%ld", &n); /* n is a long integer, so you need l */
  while(n!=0)
  {
    r=n%10;
    if(r==1)
      ones++;
    if(r==0)
      zeroes++;
    n=n/10;
  }
  printf("\nNumber of ones are %d", ones);
  printf("\nNumber of zeroes are %d", zeroes);

  /* getch(); is not portable use: */
  getchar(); /* in this case you need to call getchar() twice */

 /* add */ 
  return 0;
}

Try now.

In your first post you did good -- mentioned what you wanted and what the program actually did. This time you left it up to us to figure out what happens. always tell us
1) what you did,
2) what the program did, and
3) what you expected instead

sorry WaltP Sir, will take care of it in future...

scanf("%ld", &n); /* n is a long integer, so you need l */

Try now.

thanx a lot...this helped...!!
now the program is working fine...

I think the following method is the most efficient one.

/*
    Assume  the size of the "unsigned int" is 32
*/
unsigned char calculate1num(unsigned int num)
{
    return num - (num<<1) - (num<<2) - (num<<3) - (num<<4)
                          - (num<<5) - (num<<6) - (num<<7) - (num<<8)
                          - (num<<9) - (num<<10) - (num<<11) - (num<<12)
                          - (num<<13) - (num<<14) - (num<<15) - (num<<16)
                          - (num<<17) - (num<<18) - (num<<19) - (num<<20)  
                          - (num<<21) - (num<<22) - (num<<23) - (num<<24)
                          - (num<<25) - (num<<26) - (num<<27) - (num<<28)
                          - (num<<29) - (num<<30) - (num<<31);
}
commented: Ugliest code formatting of the week award. Next time, try [code] and not [i] -2

That's terrible... :confused:

Aia..This code is not working for longer number of binary numbers. Can you please resolve this issue ?

commented: Please make a new discussion with your new code so far. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.