ProgrammingHard
int i = 0, c = 0; while (i * i < 50) { c++; i++; } printf("%d", c); What is printed?
- A. 7
- B. 50
- C. 8
- D. 64
Correct answer: C. 8
The body runs for i = 0..7 (49 < 50) and stops when i = 8 gives 64, so c = 8 because counting starts at i = 0. 7 forgets the i = 0 pass, 50 reads the test as i < 50, and 64 is the failing value of i * i, not the count.
More C Programming questions
- int c = 0; for (int i = 1; i <= 100; i++) if (i % 7 == 0) c++; printf("%d", c); What is printed?
- int cnt = 0; for (int i = 0; i < 10; i += 3) cnt++; printf("%d", cnt); What is printed?
- int i = 0, j = 10, cnt = 0; while (i < j) { i++; j--; cnt++; } printf("%d", cnt); What is printed?
- unsigned int i; for (i = 10; i >= 0; i--) { } What is the behaviour of this loop?
- Which C code correctly counts the digits of n = 10^9 in a fast enough way for many queries?
- The sum of the digits of n = 10^9 - 1 is computed with a loop that divides by 10 each time. How many loop iterations run?