Conditional Statements

Sort by

recency

|

817 Discussions

|

  • + 0 comments

    Hey guys, here a more compact solution:

    include

    using namespace std;

    string ltrim(const string &); string rtrim(const string &);

    int main() { string n_temp; getline(cin, n_temp);

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

    }

    string ltrim(const string &str) { string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );
    
    return s;
    

    }

    string rtrim(const string &str) { string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );
    
    return s;
    

    }

  • + 0 comments

    include

    include

    using namespace std; int main() {

    int n=0;
    cin>>n;
    
    if(n>=1&&n<=pow(10,9)) {
        if(n>=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
                cout<<"nine"<<endl;
        }
        else {
            cout<<"Greater than 9"<<endl;
        }
    }
    return 0;
    

    }

  • + 0 comments

    Can i use this code in my website? Is it helpful?

  • + 0 comments

    Very Basic and Understandable code.

    int main()
    {
        string n_temp;
        getline(cin, n_temp);
    
        int n = stoi(ltrim(rtrim(n_temp)));
    
        // Write your code here
        if (n<1)cout<<"Lesser than 1";
        else 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 cout<<"Greater than 9";
        
    
        return 0;
    }
    
  • + 0 comments

    If you want a simple method (Very basic). you can use switch case. It will work just fine and also switch will come easier to write.

    include

    using namespace std;

    int main() {

    int n;
     cin>>n;
    
    
    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;
    }
    
        return 0;
    
    
    return 0;
    

    }