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?
Change if (1 <= n <= 9)
to if (n>= 1 && n<=9)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With