0
0
C++programming~10 mins

Else–if ladder 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 the number is positive.

C++
#include <iostream>
using namespace std;

int main() {
    int num = 10;
    if (num [1] 0) {
        cout << "Positive number" << 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 '>' to check positivity.
Using '<' which checks for negative numbers.
2fill in blank
medium

Complete the else-if ladder to check if a number is negative.

C++
#include <iostream>
using namespace std;

int main() {
    int num = -5;
    if (num > 0) {
        cout << "Positive" << endl;
    } else if (num [1] 0) {
        cout << "Negative" << endl;
    } else {
        cout << "Zero" << endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
B==
C>=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only checks for zero.
Using '>=' which includes zero and positive numbers.
3fill in blank
hard

Fix the error in the else-if ladder condition to correctly check for zero.

C++
#include <iostream>
using namespace std;

int main() {
    int num = 0;
    if (num > 0) {
        cout << "Positive" << endl;
    } else if (num [1] 0) {
        cout << "Zero" << endl;
    } else {
        cout << "Negative" << 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 '>' which check for negative or positive numbers.
Using '!=' which checks for not equal, not zero.
4fill in blank
hard

Fill both blanks to complete the else-if ladder that prints the number's sign.

C++
#include <iostream>
using namespace std;

int main() {
    int num = -3;
    if (num [1] 0) {
        cout << "Positive" << endl;
    } else if (num [2] 0) {
        cout << "Negative" << endl;
    } else {
        cout << "Zero" << endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the operators in the conditions.
Using '==' or '!=' in the first two conditions.
5fill in blank
hard

Fill all three blanks to create an else-if ladder that prints "Positive", "Negative", or "Zero" based on the number.

C++
#include <iostream>
using namespace std;

int main() {
    int num = 0;
    if (num [1] 0) {
        cout << "Positive" << endl;
    } else if (num [2] 0) {
        cout << "Negative" << endl;
    } else if (num [3] 0) {
        cout << "Zero" << 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 zero check.
Mixing up the order of conditions.