0
0
Cprogramming~10 mins

Nested loops in C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 3 using a for loop.

C
for(int i = 1; i [1] 3; i++) {
    printf("%d\n", i);
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= will not run the loop as expected.
Using < will stop before printing 3.
2fill in blank
medium

Complete the code to print a 3x3 grid of stars using nested loops.

C
for(int i = 0; i < 3; i++) {
    for(int j = 0; j [1] 3; j++) {
        printf("*");
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
A<
B>
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= will print 4 stars instead of 3.
Using > or >= will not run the loop correctly.
3fill in blank
hard

Fix the error in the nested loops to correctly print a 4x4 grid of numbers.

C
for(int row = 1; row <= 4; row++) {
    for(int col = 1; col [1] 4; col++) {
        printf("%d ", col);
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will stop before printing 4.
Using > or >= will cause the loop not to run properly.
4fill in blank
hard

Fill both blanks to create a nested loop that prints a right triangle of stars.

C
for(int i = 1; i [1] 5; i++) {
    for(int j = 1; j [2] i; j++) {
        printf("*");
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < in the outer loop will print only 4 lines.
Using < in the inner loop will print one less star per line.
5fill in blank
hard

Fill all three blanks to create a nested loop that prints a multiplication table from 1 to 3.

C
for(int x = 1; x [1] 3; x++) {
    for(int y = 1; y [2] 3; y++) {
        printf("%d ", x * [3]);
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
A<=
B<
Cy
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will exclude the number 3.
Using x instead of y in multiplication will print squares.