LearnFrenzy
Sign in Take a free test

C Loops · Predict the Output · Question 8

Nested loop starting at the outer index – count Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following C program?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links