Conditional Statements

Sort by

recency

|

839 Discussions

|

  • + 0 comments

    if (n ==1) { cout << "one"; } else if (n ==2){ cout << "two";

    } 
    else if (n == 3){
        cout << "three";
    }
    else if (n == 4 )
    {cout << "four ";}
    else if (n == 5 )
    {cout << "five";}
    else if (n == 6)
    {cout << "six";}
    else if (n == 7)
    {cout << "seven";}
    else if (n == 8)
    {cout << "eight";}
    else if (n == 9){
        cout << "nine";
    }
    else if (n > 9) {
        cout << "Greater than 9";
    
    }
    
  • + 0 comments
    string number_name[10]= { 
        "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "Greater than 9"
    };
    
    string n_temp;
    getline(cin, n_temp);
    
    int n = stoi(ltrim(rtrim(n_temp)));
    
    // Write your code here
    int idx = n < 10 ? n-1 : 9;
    
    cout << number_name[idx] << endl;      
    return 0;
    
  • + 0 comments

    include

    using namespace std;

    int main() { int n; cin >> n;

    string words[] = {
        "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine"
    };
    
    if (n >= 1 && n <= 9)
        cout << words[n - 1] << endl;
    else
        cout << "Greater than 9" << endl;
    
    return 0;
    

    }

  • + 0 comments

    include

    using namespace std;

    int main() { string word[10] = {"Greater than 9", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    int n;
    string s;
    
    
    getline(cin, s);
    
    stringstream ss(s);
    while (ss >> n) {
        if (n >= 1 && n <= 9)
            cout << word[n] << " ";
        else
            cout << word[0] << " ";
    }
    
    cout << endl;
    return 0;
    

    }

  • + 0 comments

    Here is Conditional Statements solution in C++ - https://programmingoneonone.com/hackerrank-conditional-statements-solution-in-cpp.html