0
0
Cprogramming~10 mins

Why loops are needed in C - Test Your Understanding

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 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'
Ai
B5
C0
D10
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.
2fill in blank
medium

Complete 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'
A<=
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <=, which stops at 9.
Using == which only runs once.
3fill in blank
hard

Fix 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'
A==
B>=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= which causes the loop not to run.
Using == which runs only once.
4fill in blank
hard

Fill 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'
A2
B1
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 instead of 2.
Using > instead of <= which skips printing 10.
5fill in blank
hard

Fill 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'
A5
B>=
C--
D++
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.