Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Statement issue in C++

Tags:

c++

Here is the question that I've been trying to solve:

You are given a positive integer, n,:

If 1 ≤ n ≤ 9, then print the English representation of it. That is "one" for 1, "two" for 2, and so on.

Otherwise print "Greater than 9" (without quotes)

Here is a portion of my suggested answer, but it doesn't work!

int n;

if (1 <= n <= 9) {
    if (n == 1) {
    cout << "one" << endl;
    } else if (n == 2) {
    cout << "two" << endl;
    } else if (n == 3) {
    cout << "three" << endl;
    } else if (n == 4) {
    cout << "four" << endl;
    } else if (n == 5) {
    cout << "five" << endl;
    } else if (n == 6) {
    cout << "six" << endl;
    } else if (n == 7) {
    cout << "seven" << endl;
    } else if (n == 8) {
    cout << "eight" << endl;
    } else if (n == 9) {
    cout << "nine" << endl;
    }
} else {
    
    cout << "Greater than 9" << endl;
}

What is the issue with the code?

like image 945
Rayan Avatar asked Dec 10 '22 18:12

Rayan


1 Answers

Change if (1 <= n <= 9) to if (n>= 1 && n<=9)

like image 69
Pooya Avatar answered Dec 30 '22 18:12

Pooya