Conditional Statements

Sort by

recency

|

846 Discussions

|

  • + 0 comments
    #include <iostream>
    using namespace std;
    
    int main() {
        int a;
        cin >> a;
        string name[] = {"0" , "one" , "two", "three" , "four", "five", "six", "seven", "eight", "nine"};
        if (a >= 1 and a <= 9) {
            cout << name[a];
        }
        else if (a > 9) {
            cout << "Greater than 9";
        }
        return 0;
    }
    
  • + 0 comments

    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)));
    
    // Write your code here
    cout<<"Converted numner"<<n<<endl;
    
    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()
    ); // can anyone solve this 
    
    
    return s;
    

    }

  • + 0 comments

    Sol using switch:

    int main()
    {
        string n_temp;
        getline(cin, n_temp);
    
        int n = stoi(ltrim(rtrim(n_temp)));
    
        switch (n) {
            case 1:
                printf("one");
                break;
            case 2:
                printf("two");
                break;
            case 3:
                printf("three");
                break;
            case 4:
                printf("four");
                break;
            case 5:
                printf("five");
                break;
            case 6:
                printf("six");
                break;
            case 7:
                printf("seven");
                break;
            case 8:
                printf("eight");
                break;
            case 9:
                printf("nine");
                break;
            default:
                printf("Greater than 9");
        
        }
    
        return 0;
    }
    
  • + 0 comments

    Exercise was ment to teach conditional statements, so I used them all without anything else which would be (cheating in my opinion).

    I changed input type int n to unsigned int n because of given constraints. It says that there can't be negative numbers, so we should use a proper type.

    I have put Greater than 9 condition before all other because it is more likely that user will input greater number than 9. This way we prevent 9 statement checks which will make code execute faster in most cases.

    This is the code:

    		unsigned int n = stoi(ltrim(rtrim(n_temp)));
    
    		if (n > 9)
            cout << "Greater than 9";
        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
            cout << "nine";
    
  • + 0 comments

    include

    using namespace std; int main(){ int n; cin>>n;

    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;
    

    }