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.

In an "If statement" what will happen if we directly give "even".Instead of giving through an array?
Example:
int i,b,c;
string a[11]={"odd","one","two","three","four","five","six","seven","eight","nine"};
cin>>c>>b;
for(i=c;i<=b;i++)
{
if((i>9) && (i%2==0))
cout<<"even"<

you have to remove the break statements otherwise you will have only one output ( of the matching case ).
if you skip the break statements you will get all the outputs after your matching statement

#include<iostream>#include<cstdio>usingnamespacestd;intmain(){// Complete the code.inta,b;intn=0;stringintMap[9]={"one","two","three","four","five","six","seven","eight","nine"};cin>>a>>b;if((a<=9)&&(b<=9)){for(n=a;n<=b;n++){cout<<intMap[n-1]<<endl;}}elseif((a<=9)&&(b>9)){for(n=a;n<=9;n++){cout<<intMap[n-1]<<endl;}for(n=10;n<=b;n++){if(n%2==0){cout<<"even"<<endl;}else{cout<<"odd"<<endl;}}}else{for(n=a;n<=b;n++){if(n%2==0){cout<<"even"<<endl;}else{cout<<"odd"<<endl;}}}return0;}

//Compares the values of a, b with 9 and if values <= 9 then//Prints all values from 'a' uptill 'b' in string formatif((a<=9)&&(b<=9)){for(n=a;n<=b;n++){cout<<intMap[n-1]<<endl;}}//Compares the values of a, b with 9//And if the value of a <= 9 and value of b > 9 then....elseif((a<=9)&&(b>9)){//Prints all the values starting from 'a' uptill 9//in string format and then....for(n=a;n<=9;n++){cout<<intMap[n-1]<<endl;}//For values which are greater than 9 and <= to 'b'//Prints 'even' or 'odd' for themfor(n=10;n<=b;n++){if(n%2==0){cout<<"even"<<endl;}else{cout<<"odd"<<endl;}}}Hopethathelps.

for(n=10;n<=b;n++)
checking the value of n is less than b
if(n%2==0)
suppose the value of n you given is 10
if statement will find the remainder by using the modulus %
that is n%2
10%2 just divide the reminder will always be zero which means if its divisible by 2 it is even number..

you have a for loop and it start counting from 10 , after check the condition if n is inferior or egal to b , when it is true it increment the value of n ...
if n%2 == 0 , means if n is divisible by 2 , he will display even
I hope it helps you :-)

can u please explain the string type in C++ .I searched it over net but didn't got the satisfying result. According to me String is an array of char but you used it as a predefined data-type.how?

After your "else if", your "else" statement can be skipped. It will pass all tests cases without. Just saying...
Your code is great by the way and pass all tests cases anyway ;-)

## For Loop

You are viewing a single comment's thread. Return to all comments →

Thank you. This really clarified things. Rewrote and got the code running in about 30 seconds after reading this.

Thanx,it really helped..

thanks,it really Helped more

Thanx, it tooks me also a little bit of time before understanding what was the purpose of this exercice. It's not clear.

thank you,its been helpful

## include

## include

using namespace std; int main() { int a,b; cin>>a>>b; for(a;a<=b;a++) {

if(a<=9) { if(a==8) cout<<"eight\n"; else if(a==9) cout<<"nine\n"; else if(a==7) cout<<"seven\n"; else if(a==6) cout<<"six\n"; else if(a==5) cout<<"five\n"; else if(a==4) cout<<"four\n"; else if(a==3) cout<<"three\n"; else if(a==2) cout<<"two\n"; else if(a==1) cout<<"one\n"; } else {

} return 0; }

its easier and better looking to use a switch statement: switch (count){ case 1: cout << "one" << endl; break; case 2: cout << "two" << endl; break; 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: if (count%2 == 0) cout << "even" << endl; else cout << "odd" << endl;

exactly...but here the quesion is realted to loop so we should ans according to the question.

but the questionnis looking for the numbers(in words) in order. so if you use break statement they wont outcome in order.

This is my code

nice bruh!

in your code why are u useing the variable c,b;

b and c are variables to take 2 inputs.

actually its use for take input.

thanks man i was missing somnething out. my code is the same. arrays solved it in some lines of code!

What if b>c?? it wont work for b>c test case

that's great man, you have clarified my doubt. thanks a lot

thnkx

perfect....!

nice code

thanks bro u cracked it

In an "If statement" what will happen if we directly give "even".Instead of giving through an array? Example: int i,b,c; string a[11]={"odd","one","two","three","four","five","six","seven","eight","nine"}; cin>>c>>b; for(i=c;i<=b;i++) { if((i>9) && (i%2==0))

cout<<"even"<

sweet

sweet

the initializer for string didn't work out with me

Nice work!

Nice

A simplified way

thank u so much.

you have to remove the break statements otherwise you will have only one output ( of the matching case ). if you skip the break statements you will get all the outputs after your matching statement

thanks this works in c++. I get problems with cin undclared if I used C

same here

c dosnt hace cin u should use scanf

Great code, I like it.

Here is mine:

for(n=10;n<=b;n++){

if (n%2==0){ cout<<"even"<

why you have used intmap[n-1]

thank you so much...

for(n=10;n<=b;n++) checking the value of n is less than b if(n%2==0) suppose the value of n you given is 10 if statement will find the remainder by using the modulus % that is n%2 10%2 just divide the reminder will always be zero which means if its divisible by 2 it is even number..

you have a for loop and it start counting from 10 , after check the condition if n is inferior or egal to b , when it is true it increment the value of n ... if n%2 == 0 , means if n is divisible by 2 , he will display even I hope it helps you :-)

what is the use of String intMap[]

can u please explain the string type in C++ .I searched it over net but didn't got the satisfying result. According to me String is an array of char but you used it as a predefined data-type.how?

are you askin about its library file? If that is true,

## include

Love your code but arrays havent been introduced here yet! so why did u use it? Cant it be done without arrays?

After your "else if", your "else" statement can be skipped. It will pass all tests cases without. Just saying... Your code is great by the way and pass all tests cases anyway ;-)

Much simple, try this.

it helped

Unnecessarily making it so long and unreadable.

I think it is better than writing multiple "if-else" and one who can code can read it without any trouble.

what for the values when a and b both are greater than 9

This works but is not correct, it will not work if the numbers greater than 9 are fewer than the numbers that are les than or equal to 9.

for example a=3 b=5 out put would be; three four five

no odds or evens

Thanks a lot

Don't waste your time typing all that out

you could also use an arry insted on usong all these lines

Thanks! this helped a lot.