Question 6
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int main(void) {
for (int k = 0; k < 3; k++) {
int total = 0;
total += k;
printf("%d ", total);
}
printf("\n");
return 0;
}
- A 0 1 2 Correct
- B 0 1 3
- C 3 3 3
- D 0 0 0
Correct Answer: Option A
total is created and set to 0 at the start of every pass, so it never accumulates. To keep a running total, declare it before the loop.
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!