0
0
C++programming~10 mins

If–else statement 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++
int num = 5;
if (num [1] 0) {
    std::cout << "Positive" << std::endl;
}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' to check positivity.
Using '<' which checks for negative numbers.
2fill in blank
medium

Complete the code to print "Even" if the number is even.

C++
int num = 4;
if (num % 2 [1] 0) {
    std::cout << "Even" << std::endl;
}
Drag options to blanks, or click blank then click option'
A<
B!=
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which checks for odd numbers.
Using '>' or '<' which are not suitable here.
3fill in blank
hard

Fix the error in the if–else statement to print "Positive" or "Non-positive".

C++
int num = -3;
if (num [1] 0) {
    std::cout << "Positive" << std::endl;
} else {
    std::cout << "Non-positive" << std::endl;
}
Drag options to blanks, or click blank then click option'
A==
B<=
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' which reverses the logic.
Using '==' which only checks for zero.
4fill in blank
hard

Fill both blanks to print "Adult" if age is 18 or more, otherwise "Minor".

C++
int age = 20;
if (age [1] 18) {
    std::cout << "Adult" << std::endl;
} else {
    std::cout << "[2]" << std::endl;
}
Drag options to blanks, or click blank then click option'
A>=
B<
CMinor
DAdult
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' for the condition.
Printing "Adult" in the else block.
5fill in blank
hard

Fill all three blanks to print "Positive", "Negative", or "Zero" based on the value of num.

C++
int num = 0;
if (num [1] 0) {
    std::cout << "Positive" << std::endl;
} else if (num [2] 0) {
    std::cout << "Negative" << std::endl;
} else {
    std::cout << "[3]" << std::endl;
}
Drag options to blanks, or click blank then click option'
A>
B<
CZero
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which does not distinguish zero.
Mixing up the comparison operators.