What is the output of the following 𝐶 program? # include <stdio.h> main () {…

2019

What is the output of the following 𝐶 program?
# include <stdio.h>
main ()
{
int i, j, x=0;
for (i=0; i<5; ++i)
for (j=0; j<i; ++j)
{
x+=(i+j-1);
break;
}
printf(“%d”, x);
}

  1. A.

    6

  2. B.

    5

  3. C.

    4

  4. D.

    3

Attempted by 470 students.

Show answer & explanation

Correct answer: A

Solution: Evaluate the nested loops and the effect of the break.

  • i = 0: inner loop does not run, contribution = 0

  • i = 1: inner loop runs once with j = 0, contribution = 1 + 0 - 1 = 0

  • i = 2: inner loop runs once with j = 0, contribution = 2 + 0 - 1 = 1

  • i = 3: inner loop runs once with j = 0, contribution = 3 + 0 - 1 = 2

  • i = 4: inner loop runs once with j = 0, contribution = 4 + 0 - 1 = 3

Total x = 0 + 0 + 1 + 2 + 3 = 6

Final output: 6

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor