Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' causes the loop to never run.
Using '>' reverses the loop condition incorrectly.
✗ Incorrect
The loop should run while i is less than 3 to print numbers 1 and 2. Using '<' is correct.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes 4 stars per line.
Using '>=' or '>' causes the loop to not run.
✗ Incorrect
The inner loop should run while j is less than 3 to print 3 stars per line.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses the last multiplication (when j=3).
Using '>' or '>=' causes the loop to not run properly.
✗ Incorrect
Using '<=' ensures the inner loop runs for j = 1, 2, 3 to print the full table.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Both loops use '<=' to include the last value and print the correct number of stars per line.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes loops to run only 3 times.
Using '*' instead of '+' changes the output meaning.
✗ Incorrect
Both loops use '<=' to include 4, and '+' combines row and col numbers for output.