binary.c (901B)
1 #include <math.h> 2 #include <stdio.h> 3 4 // binary: Test the user on the binary powers of 2. 5 void binary() { 6 int power = 0; 7 int max_power = 64; 8 while (power <= max_power) { 9 printf("What is 2 to the power of %u?\n", power); 10 11 // Make sure there is no input buffer overflow. 12 char line[256]; 13 if (fgets(line, sizeof(line), stdin)) { 14 15 unsigned long long answer; 16 if (sscanf(line, "%llu", &answer) == 1) { 17 printf("You have entered: %llu\n", answer); 18 if (answer == pow(2, power)) { 19 printf("You are correct!\n\n"); 20 power++; 21 } else { 22 printf("Incorrect. The answer is %.0f." 23 "\n", pow(2, power)); 24 return; 25 } 26 } else { 27 printf("\nYou have entered a non-integer " 28 "answer. Please try again.\n"); 29 } 30 } 31 } 32 printf("\n\nCongrats! You are highly skilled in the powers of 2.\n\n"); 33 } 34 35 int main(int argc, char* argv[]) { 36 binary(); 37 return 0; 38 } 39