# include # include int main ( ) { // // We will use B to store the digits of our binary number. // Since we have 5 digits, we can represent 0 to 31. // int b[5] = { 0, 0, 0, 0, 0 }; bool done = false; int i; int n = 5; int value = 0; // // B starts out representing 0. Print it. // while ( !done ) { printf ( "%2i %i%i%i%i%i\n", value, b[0], b[1], b[2], b[3], b[4] ); value = value + 1; // // Now see if there's a number that comes after this number. // // Starting at the rightmost digit in B, which is B[N-1], ... // if the digit is 0, make it a 1, and you're done (BREAK!). // if the digit is 1, make it a 0, and move left one position; // done = true; for ( i = n - 1; 0 <= i; i-- ) { if ( b[i] == 0 ) { b[i] = 1; done = false; break; } b[i] = 0; } // // If we never found a 0, then B was entirely 1's. So we're done. // Luckily, that means DONE is TRUE. So the WHILE loop will terminate. // } return 0; }