C++ Switch Case Statement | Practice | GeeksforGeeks

Given a number N, if the number is between 1 and 10 both inclusive then return the number in words (Lower case English Alphabets) otherwise return "not in range".
 
Example 1:
Input:
5
Output:
five
 
Example 2:
Input:
11
Output:
not in range
 
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isInRange() which takes an integer and if the number is between 1 and 10 both inclusive then return the number in words otherwise return "not in range".
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
1<=N<=10000
 
0
avijit1126be203 weeks ago
class Solution{   static String isInRange(int N){       // code here       String str = "";       switch(N)       {           case 1:               str = "one";               break;           case 2:               str = "two";               break;           case 3:               str = "three";               break;           case 4:               str = "four";               break;           case 5:               str = "five";               break;           case 6:               str = "six";               break;           case 7:               str = "seven";               break;           case 8:               str = "eight";               break;           case 9:               str = "nine";               break;           case 10:               str = "ten";               break;           default:               str = "not in range";       }       return str;          }}
0
anushavennapoosa20021 month ago
// JAVA SOLUTION
class Solution{   static String isInRange(int N){       // code here
     switch(N){      case 1: return "one";
      case 2: return "two";
      case 3: return "three";
      case 4: return "four";
      case 5: return "five";
      case 6: return "six";
      case 7: return "seven";
      case 8: return "eight";
      case 9: return "nine";
      case 10: return "ten";
      default: return "not in range";  }
   }}
0
shorouqehab0761 month ago
#include<iostream>
using namespace std;int main(){   int x;   cin >> x;   switch (x) {   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;       case 10: cout << "ten";           break;   default: cout << "not in range";   }   return 0;}
0
shorouqehab0761 month ago
#include<iostream>
using namespace std;int main(){   int x;   cin >> x;   switch (x) {   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;       case 10: cout << "ten";           break;   default: cout << "not in range";   }   return 0;}
0
shorouqehab0761 month ago
#include<iostream>
using namespace std;int main(){   int x;   cin >> x;   switch (x) {   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;       case 10: cout << "ten";           break;   default: cout << "out of range";   }   return 0;}
0
imranmbhd24122 months ago
###CPP IMRAN
string isInRange(int N){       string a[] = {           "one", "two", "three", "four", "five", "six", "seven", "eight",           "nine", "ten"       };              if(N>= 1 && N<=10) return a[N-1];       return "not in range";   }
0
0niharika22 months ago
switch(N){       case 1: return "one";
       case 2: return "two";
       case 3: return "three";
       case 4: return "four";
       case 5: return "five";
       case 6: return "six";
       case 7: return "seven";
       case 8: return "eight";
       case 9: return "nine";
       case 10: return "ten";
       default: return "not in range";   }
0
arunuppal20032 months ago
#include <iostream>
using namespace std;
int main(){       int a;       cout<<"Enter the number";       cin>>a;       switch(a){           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;            case 10:           cout<<"ten";           break;           default:           cout<<"not in range";                  }
 
   return 0;}
0
tonroyjptt3 months ago
(switch(N){           case 1: cout<<"one"<<endl;           break;
          .....
          default:cout<<"not in range":
          break;)
}
write code like this might gives you errors in more test cases... but not in available test cases...
 
write like this----
    string isInRange(int N){       // code here               switch(N){           case 1: //cout<<"one"<<endl;               return "one";           break;           case 2: //cout<<"two"<<endl;               return "two";           break;           case 3: //cout<<"three"<<endl;               return "three";           break;           case 4: //cout<<"four"<<endl;               return "four";           break;           case 5: //cout<<"five"<<endl;               return "five";           break;           case 6: //cout<<"six"<<endl;               return "six";           break;           case 7: //cout<<"seven"<<endl;               return "seven";           break;           case 8: //cout<<"eight"<<endl;               return "eight";           break;           case 9: //cout<<"nine"<<endl;               return "nine";           break;           case 10: //cout<<"ten"<<endl;               return "ten";           break;           default: //cout<<"not in range";               return "not in range";           break;       }   }
+1
anilkumarug213 months ago
My code is right but showing incorrect
// { Driver Code Starts#include<bits/stdc++.h> using namespace std;
// } Driver Code Endsclass Solution{   public:   string isInRange(int N){       // code here        int N;        cin>>N       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 ;            case 10:            cout<< " ten ";            break ;            default:            cout <<" not  in  range ";            break ;                  }   }};
// { Driver Code Starts.int main() {    int t;   cin>>t;   while(t--)   {       int N;       cin >> N;       Solution ob;       cout << ob.isInRange(N) << endl;   }   return 0; }   // } Driver Code Ends
 
it is better  to give a blank coding screen😠😡
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.