Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print numbers from 1 to 5 using a loop.
C
#include <stdio.h> int main() { int i = 1; while ([1] <= 5) { printf("%d\n", i); i++; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number instead of the loop variable in the condition.
Not using the variable that changes inside the loop.
✗ Incorrect
The variable i is used to control the loop and print numbers from 1 to 5.
2fill in blank
mediumComplete the code to sum numbers from 1 to 10 using a for loop.
C
#include <stdio.h> int main() { int sum = 0; for (int i = 1; i [1] 10; i++) { sum += i; } printf("Sum is %d\n", sum); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <=, which stops at 9.
Using == which only runs once.
✗ Incorrect
The loop should include 10, so the condition uses <= to go up to 10.
3fill in blank
hardFix the error in the loop condition to print numbers from 0 to 4.
C
#include <stdio.h> int main() { int count = 0; while (count [1] 5) { printf("%d\n", count); count++; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= which causes the loop not to run.
Using == which runs only once.
✗ Incorrect
The loop should run while count is less than 5 to print 0 to 4.
4fill in blank
hardFill both blanks to create a loop that prints even numbers from 2 to 10.
C
#include <stdio.h> int main() { for (int num = [1]; num [2] 10; num += 2) { printf("%d\n", num); } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 instead of 2.
Using > instead of <= which skips printing 10.
✗ Incorrect
The loop starts at 2 and runs while num is less than or equal to 10 to print even numbers.
5fill in blank
hardFill all three blanks to create a loop that prints numbers from 5 down to 1.
C
#include <stdio.h> int main() { for (int i = [1]; i [2] 1; i[3]) { printf("%d\n", i); } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ instead of -- which counts up instead of down.
Using < instead of >= which causes the loop to not run.
✗ Incorrect
The loop starts at 5, runs while i is greater than or equal to 1, and decreases i by 1 each time.