0
0
C++programming~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 2 using a for loop.

C++
for (int i = 1; i [1] 3; i++) {
    std::cout << i << std::endl;
}
Drag options to blanks, or click blank then click option'
A>
B<=
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' causes the loop to never run.
Using '>' reverses the loop condition incorrectly.
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++) {
        std::cout << "*";
    }
    std::cout << std::endl;
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes 4 stars per line.
Using '>=' or '>' causes the loop to not run.
3fill in blank
hard

Fix the error in the nested loops to print a multiplication table from 1 to 3.

C++
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j [1] 3; j++) {
        std::cout << i * j << " ";
    }
    std::cout << std::endl;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses the last multiplication (when j=3).
Using '>' or '>=' causes the loop to not 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++) {
        std::cout << "*";
    }
    std::cout << std::endl;
}
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 causes only 4 lines.
Using '<' in the inner loop causes one less star per line.
5fill in blank
hard

Fill all three blanks to create nested loops that print a 4x4 grid with row and column numbers.

C++
for (int row = 1; row [1] 4; row++) {
    for (int col = 1; col [2] 4; col++) {
        std::cout << row [3] col << " ";
    }
    std::cout << std::endl;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes loops to run only 3 times.
Using '*' instead of '+' changes the output meaning.