Conditional Statements

Sort by

recency

|

838 Discussions

|

  • + 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

  • + 0 comments
    #include<iostream>
    using namespace std;
    int main(){
            int n;
            cin>>n;
            if(n==1) cout<<"one";
            if(n==2) cout<<"two";
            if(n==3) cout<<"three";
            if(n==4) cout<<"four";
            if(n==5) cout<<"five";
            if(n==6) cout<<"six";
            if(n==7) cout<<"seven";
            if(n==8) cout<<"eight";
            if(n==9) cout<<"nine";
            if(n>9) cout<<"Greater than 9";
    
        return 0;   
    }