0
0
C++programming~10 mins

Nested conditional statements 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 check if a number is positive.

C++
#include <iostream>
int main() {
    int num = 5;
    if (num [1] 0) {
        std::cout << "Positive" << std::endl;
    }
    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 '>' will check for negative numbers.
Using '==' checks for equality, not positivity.
2fill in blank
medium

Complete the code to check if a number is between 1 and 10 (inclusive).

C++
#include <iostream>
int main() {
    int num = 7;
    if (num [1] 1 && num [2] 10) {
        std::cout << "In range" << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' excludes the boundary numbers.
Using '||' instead of '&&' allows numbers outside the range.
3fill in blank
hard

Fix the error in the nested if statement to check if a number is positive and even.

C++
#include <iostream>
int main() {
    int num = 4;
    if (num > 0) {
        if (num [1] 2 == 0) {
            std::cout << "Positive and even" << std::endl;
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A+
B/
C*
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' divides the number but does not check evenness.
Using '*' or '+' are arithmetic operations unrelated to checking evenness.
4fill in blank
hard

Fill both blanks to complete the nested conditional that prints if a number is positive and divisible by 3.

C++
#include <iostream>
int main() {
    int num = 9;
    if (num [1] 0) {
        if (num [2] 3 == 0) {
            std::cout << "Positive and divisible by 3" << std::endl;
        }
    }
    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 '>' for positivity.
Using '/' instead of '%' for divisibility check.
5fill in blank
hard

Fill all three blanks to create a nested conditional that prints if a number is negative, odd, and less than -10.

C++
#include <iostream>
int main() {
    int num = -15;
    if (num [1] 0) {
        if (num [2] 2 != 0) {
            if (num [3] -10) {
                std::cout << "Negative, odd, and less than -10" << std::endl;
            }
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
B%
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for negativity or less than -10.
Using '/' instead of '%' for oddness check.