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();
}

Dani AI

Generated

Two common pitfalls here are: (1) converting the input to an integer loses leading zeros (so "00101" becomes 101), and (2) long binary strings can overflow the integer type, producing wrong counts. If you want to count digits exactly as the user typed them, read characters and count them directly instead of converting to a number.

Example that accepts arbitrarily long input and validates only 0/1:

#include <stdio.h>

int main(void) {
    int c;
    unsigned long long ones = 0, zeros = 0;
    int saw_digit = 0;

    printf("Enter a binary number: ");
    fflush(stdout);

    while ((c = getchar()) != '\n' && c != EOF) {
        if (c == '0') { zeros++; saw_digit = 1; }
        else if (c == '1') { ones++; saw_digit = 1; }
        else { fprintf(stderr, "Error: non-binary digit '%c'\n", c); return 1; }
    }

    if (!saw_digit) { fprintf(stderr, "No input\n"); return 1; }

    printf("Number of ones: %llu\nNumber of zeroes: %llu\n", ones, zeros);
    return 0;
}

If instead you truly want the bit count of a numeric value (not the typed digits), use a bitwise method after parsing to an unsigned type. Brian Kernighan’s trick is portable and fast: for (count = 0; n; n &= n - 1) count++;. Many compilers also expose popcount intrinsics, e.g., GCC’s __builtin_popcount family (GCC docs). Note that bit counts depend on the type’s width and ignore leading zeros in the textual input.

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.