Conditional Statements

Sort by

recency

|

824 Discussions

|

  • + 0 comments

    This code can't run in hackerrank but it run in codeblock and online compiler with same output.plz someone solve this if i am wrong.

     #include <iostream>
    

    using namespace std;

    int main() { int n; cout << "Enter any 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 nine" << endl;
    }
    
    return 0;
    

    }

  • + 0 comments
    int n = stoi(ltrim(rtrim(n_temp)));
    if(n>=1 && n<=9){
        cout<<static_cast<char>(n);
    }else{
        cout<<"Greater than 9.";
    } 
        why does this didn't worked??
    
  • + 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 if ( n >9 ) { cout <<"Greater than 9";

    }

    return 0;
    

    }

  • + 0 comments

    string nums[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    if (n >= 1 && n <= 9)
    cout << nums[n-1];
    else
    cout << "Greater than 9";
    
  • + 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
    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";
    }
    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;
    

    }