Conditional Statements

Sort by

recency

|

833 Discussions

|

  • + 0 comments

    include

    using namespace std;

    int main(){ int n; cin>>n; string arr[10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; if(n>9)cout<<"Greater than 9"; else cout<

    }

  • + 0 comments

    I know this is supposed to train your if/else conditons and the task has constraints by giving you input how it wants it to but solution like this could save you unnecessary lines of code:

    #include <iostream>
    #include <string>
    #include <array>
    
    int main() {
        std::string input;
        std::getline(std::cin, input);
        int n = std::stoi(input); // Assumes valid input per problem constraints
    
        constexpr std::array words = {"one", "two", "three", "four", "five", 
                                      "six", "seven", "eight", "nine"};
    
        if (n >= 1 && n <= 9) {
            std::cout << words[n - 1];
        } else if (n > 9) {
            std::cout << "Greater than 9";
        }
    
        return 0;
    }
    
  • + 0 comments

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

    int n = stoi(ltrim(rtrim(n_temp)));
    
    // Write your code here
    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;
    

    }

  • + 2 comments

    int n; cin >> n;

    if (n >= 1 && n <= 9) {
        switch(n) {
            case 1: cout << "one"; break;
            case 2: cout << "two"; break;
            case 3: cout << "three"; break;
            case 4: cout << "four"; break;
            case 5: cout << "five"; break;
            case 6: cout << "six"; break;
            case 7: cout << "seven"; break;
            case 8: cout << "eight"; break;
    
    
    
            case 9: cout << "nine"; break;
        }
    } else {
        cout << "Greater than 9";
    }
    
    • + 0 comments

      use the ||(OR) operator instead

    • + 0 comments

      adding else rather than use default statement

  • + 0 comments
        switch (n) {
         case 1:
          cout << "one";
          break;
         case 2:
          cout << "two";
          break;
         case 3:
          cout << "three";
          break;
         case 4:
          cout << "four";
          break;
         case 5:
          cout << "five";
          break;
         case 6:
          cout << "six";
          break;
         case 7:
          cout << "seven";
          break;
         case 8:
          cout << "eight";
          break;
         case 9:
          cout << "nine";
          break;
         default:
          cout << "Greater than 9";
        }