title: Using C, how can I write code to count how many 1's are in an inputted integer's binary form?
body: <p>I've been doing some homework on basically just converting an inputted integer into it's binary form. So far I've made great progress on converting the inputted integer into a binary number as well as declining any number that is negative.</p>
  
  <p>The problem I'm having however is counting the number of 1's within the binary code and printing how many one's are in the binary representation. I've tried many examples of code from the internet and my book(Not direct copy and paste, I've tried fitting it to my needs) but so far have come to no avail. I believe it's cause I'm not focusing on a specific number.</p>
  
  <p>Basically, I'm new to C, very new. I've done most of my past 2 semesters in Java and have NO programming experience in any other language, just looking for a helping hand. Just to re-iterate the question:</p>
  
  <p>How can I count the number of one's in an inputted binary representation?</p>
  
  <p>Example: Inputted 44, binary is 101100
  Number of 1's: 3</p>
  
  <p>EDIT: So far with the implemented code my new problem now is that whatever integer I input, the output remains 12.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void)
  {
       int integer; // number to be entered by user
       int i, b, v;
       unsigned n = 0;
       while(v) {
          ++n;
          v &amp;= v - 1;
        }
       printf("Please type in a decimal integer\n"); // prompt
       fflush(stdout);
       scanf("%d", &amp;integer); // read an integer
  
        if(integer &lt; 0)
  {
      printf("Input value is negative!"); // if integer is less than
      fflush(stdout);
  
      return;                  // zero, print statement
  }
       else{
      printf("Binary Representation:\n", integer);
      fflush(stdout);}
  
    for(i = 31; i &gt;= 0; --i) //code to convert inputted integer to binary form
  {
      b = integer &gt;&gt; i;
      if(b&amp;1){
      printf("1");
      fflush(stdout);
      }
      else{
      printf("0");
      fflush(stdout);
      }
      }
       printf("\n");
       fflush(stdout);
       printf("Number of 1's in Binary Representation:%d\n", n);
       fflush(stdout);
       printf("\n");
       fflush(stdout);
       return 0;
  
  }//end function main
  </code></pre>