/* ------------------------------------------------------------------------ // Figure 4.11: Halving and Counting // Demonstrate the usage of assignment-combination operators. // ------------------------------------------------------------------------ // Each time we halve a number, we eliminate one binary digit. // If we halve it until it reaches zero and count the iterations, // we know how many binary digits are in the number. */ #include void main( void ) { int k; /* Loop counter */ int n; /* Input - the number to analyze. */ puts( "\n Halving and Counting\n " ); printf( " Enter an integer: " ); scanf( "%i", &n ); printf( " Your number is %i,", n ); k=0; /* Initialize the counter before the loop. */ while(n > 0) { /* Eliminate one binary digit each time around loop. */ n /= 2; /* Divide n in half and discard the remainder. */ k += 1; /* Count the number of times you did this. */ } printf( " it has %i bits when written in binary. \n\n", k ); }