0
0
Cprogramming~10 mins

For loop 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 0 to 4.

C
for (int i = 0; i [1] 5; 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 cause the loop to not run as expected.
Using <= will print an extra number.
2fill in blank
medium

Complete the code to sum numbers from 1 to 5 using a for loop.

C
int sum = 0;
for (int i = 1; i [1] 6; i++) {
    sum += i;
}
printf("%d\n", sum);
Drag options to blanks, or click blank then click option'
A>=
B<=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= 5 instead of < 6 is correct but not in the options.
Using > or >= will cause the loop to not run.
3fill in blank
hard

Fix the error in the for loop condition to print even numbers from 2 to 10.

C
for (int i = 2; i [1] 10; i += 2) {
    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 < 10 will exclude 10 from the output.
Using > or >= will cause the loop to not run.
4fill in blank
hard

Fill both blanks to create a for loop that prints numbers from 10 down to 1.

C
for (int i = [1]; i [2] 1; i--) {
    printf("%d\n", i);
}
Drag options to blanks, or click blank then click option'
A10
B>
C>=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will not print numbers down from 10.
Using > excludes 1 from the output.
5fill in blank
hard

Fill all three blanks to create a for loop that prints squares of numbers from 1 to 5.

C
for (int [1] = 1; [2] [3] 5; [1]++) {
    printf("%d\n", [1] * [1]);
}
Drag options to blanks, or click blank then click option'
Ai
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop causes errors.
Using < instead of <= excludes 5.