427 views
12 12 votes

What is the output of the following code?

#include <stdio.h>

int main() {
    int i, j, count = 0;

    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= i; j++) {
            count = count + j;
        }
    }

    printf("%d", count);
    return 0;
}

2 Answers

3 3 votes

For $\texttt{i = 1}$, the inner loop runs for $\texttt{j = 1}$, so $\texttt{count = 0 + 1 = 1}$.

For $\texttt{i = 2}$, the inner loop runs for $\texttt{j = 1, 2}$, so $\texttt{count = 1 + 1 + 2 = 4}$.

For $\texttt{i = 3}$, the inner loop runs for $\texttt{j = 1, 2, 3}$, so $\texttt{count = 4 + 1 + 2 + 3 = 10}$.

Therefore, the output is $\texttt{10}$.

Answer:
Position:
Show:

Related questions

12 12 votes
1 1 answer
355
355 views
GO Classes asked Jun 3
355 views
What is the output of the following code?#include <stdio.h int main() { int i, j, sum = 0; for (i = 1; i <= 4; i++) { for (j = 1; j <= 4; j++) { if (j == i) continue; if ...
9 9 votes
1 1 answer
291
291 views
GO Classes asked Jun 3
291 views
What is the output of the following code?#include <stdio.h int main() { int x = 2, y = 0; switch (x) { case 1: y = y + 1; case 2: y = y + 2; case 3: y = y + 3; break; def...
10 10 votes
3 3 answers
404
404 views
GO Classes asked Jun 3
404 views
What is the output of the following code?#include <stdio.h int main() { int a = 1, b = 2, c = 3; if (a++ 1 && ++b 2 || c++ == 3) printf("%d %d %d", a, b, c); else print...
11 11 votes
2 2 answers
332
332 views
GO Classes asked Jun 3
332 views
What is the output of the following code?#include <stdio.h int main() { int x = 3, y = 4, z; z = x++ + ++y; printf("%d %d %d", x, y, z); return 0; }$\texttt{4\ 5\ 8}$ $\t...