0
0
Cprogramming~10 mins

While 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 1 to 5 using a while loop.

C
#include <stdio.h>

int main() {
    int i = 1;
    while ([1]) {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai >= 6
Bi > 6
Ci == 6
Di < 6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never becomes false, causing an infinite loop.
Using the wrong comparison operator.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a while loop.

C
#include <stdio.h>

int main() {
    int sum = 0;
    int num = 1;
    while ([1]) {
        sum += num;
        num++;
    }
    printf("Sum is %d\n", sum);
    return 0;
}
Drag options to blanks, or click blank then click option'
Anum < 10
Bnum <= 10
Cnum == 10
Dnum > 10
Attempts:
3 left
💡 Hint
Common Mistakes
Stopping the loop before reaching 10, missing the last number.
Using a condition that never ends.
3fill in blank
hard

Fix the error in the while loop condition to avoid an infinite loop.

C
#include <stdio.h>

int main() {
    int count = 5;
    while ([1]) {
        printf("%d\n", count);
        count--;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Acount > 0
Bcount >= 5
Ccount < 5
Dcount == 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never becomes false.
Using equality which causes the loop to run only once.
4fill in blank
hard

Fill both blanks to create a while loop that prints even numbers from 2 to 10.

C
#include <stdio.h>

int main() {
    int num = [1];
    while (num [2] 10) {
        printf("%d\n", num);
        num += 2;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A2
B<=
C<
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 instead of 2.
Using a condition that excludes 10.
5fill in blank
hard

Fill all three blanks to create a while loop that counts down from 10 to 1 and prints each number.

C
#include <stdio.h>

int main() {
    int count = [1];
    while (count [2] 0) {
        printf("%d\n", [3]);
        count--;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A0
Bcount
C>
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect start value.
Using wrong comparison operator.
Printing a wrong variable.