Question 8
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int count = 0;
for (int i = 0; i < 4; i++)
for (int j = i; j < 4; j++)
count++;
printf("%d\n", count);
return 0;
}
- A 10 Correct
- B 16
- C 6
- D 4
Correct Answer: Option A
The inner loop runs 4, 3, 2 and 1 times: 4 + 3 + 2 + 1 = 10 = n(n + 1)/2. This "triangular" pattern appears in bubble sort and pair comparisons.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!