We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
since it involved int and char type , i decided to solve it using switch statement, and it worked on all cases, i struggledd when i was dealng with "default" case cause i decided to jusst skip it first and use a nested if condition for greater then 9 condition but then i infused it in default case using ternary operator and it worked :)
include
include
using namespace std;
int main() {
int n1,n2;
cin >> n1 >>n2;
for( int i =n1;i<=n2;i++)
{
switch(i)
{
case 1:
cout << "one"<<endl;
break;
case 2:
cout << "two"<<endl;
case 3:
cout << "three"<<endl;
break;
case 4:
cout << "four"<<endl;
break;
case 5:
cout << "five" << endl;
break;
case 6:
cout << "six"<< endl;
break;
case 7:
cout << "seven"<<endl;
break;
case 8:
cout << "eight" << endl;
break;
case 9:
cout << "nine" << endl;
break;
default:
cout << (i%2==0 ? "even" : "odd" )<< endl;
}
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
For Loop
You are viewing a single comment's thread. Return to all comments →
since it involved int and char type , i decided to solve it using switch statement, and it worked on all cases, i struggledd when i was dealng with "default" case cause i decided to jusst skip it first and use a nested if condition for greater then 9 condition but then i infused it in default case using ternary operator and it worked :)
include
include
using namespace std;
int main() { int n1,n2; cin >> n1 >>n2;
}