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.

Ok, this took me the better part of an hour to figure out what they are asking in this question. Basically, the idea is you are given two numbers 'a' and 'b' such that a<=b. You then need to test every number from a to b and output either the text version of the variable (if number <=9) or whether the value is even or odd (if number > 9).

So basically when the inputs are 8,11 you need to test every integer from 8 to 11, so you need to test 8,9,10,& 11. This leads to the output specified in the "Sample Output" of "eight, nine, even, odd". Hopefully this helps someone out.

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 ;-)

That's because the author made the problem over-complicated. This is supposed to be an introductroy style problem and he went way over the mark. I was able to read the problem because I have a minor in math and have studied number and set theory in discrete mathematics.

Try changing "else if(a==9)" to just an else statement because if/else chains always need to end with an else statement to default to if no other case is true.

That is not true. It is perfectly acceptable to have an if/if else with no last else case if your intention is to have a situation where no statement executes if your specific conditions are not met. It is analogous to writing...

Good practice would be to not assume such things, so your intuition is spot on. A simple check at the beginning of the function hurts nothing, as so...

inta,b;cin>>a;cin>>b;if((b-a)<0)return0;//or whatever other error handling is needed

Pretty poor solution. Unnecessary use of two for loops. You can do it in one for loop. The code would be easier to read if it was written using one loop and in the loop had an initial if/else structure that determines if the current number in the iteration is less than or equal to 9. If the count is less than 10, you have if statements that print the english word out, and if the count is greater than or equal to 10, then you do modulo to determine even or odd.

It is kind of poorly written, but should give the right results. It can certainly be done all in one for loop though, and in my opinion, using a string array is much cleaner than using a switch statement.

I see this kind of solution in many cases. But in fact this is wrong. This works only in case the first input is less than the second one. If the test case would be 12 and 11 then this failed the test case because the code does not enter into the for loop, because c is greater than b.

i m still a bit confused... if (no<=9) the text version of it must be got so if input is 8.. we got to get eight.. thats alrite but after that the condition goes like if(no>9) then print even or odd respective of the no.. so for input 11 we must print it as odd only right? the output must be like "eight odd" only no, how does the ouput cums like "eight nine, even,odd" ???

Yes, if the number is less or equal than 9, then print the English representation of it. If it's bigger, you should print "even" or "odd", depending on the number.
Now, your input contains two numbers. You have to print every number that is between those numbers, including those two numbers. For example, input is 8 and 11 - you print:

this assignment was not clear at all, what they should say is print from the first given number to 9 in the form of "one", "two" ... "nine", and "even" , "odd", "even", to the second given number.

## For Loop

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

Ok, this took me the better part of an hour to figure out what they are asking in this question. Basically, the idea is you are given two numbers 'a' and 'b' such that a<=b. You then need to test every number from a to b and output either the text version of the variable (if number <=9) or whether the value is even or odd (if number > 9).

So basically when the inputs are 8,11 you need to test every integer from 8 to 11, so you need to test 8,9,10,& 11. This leads to the output specified in the "Sample Output" of "eight, nine, even, odd". Hopefully this helps someone out.

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.

Great job

nice code bro

Very slight improvement to your if-then-else:

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.

Thank you!

thank you...

I could not make sense of the original question as written. Thank you.

That's because the author made the problem over-complicated. This is supposed to be an introductroy style problem and he went way over the mark. I was able to read the problem because I have a minor in math and have studied number and set theory in discrete mathematics.

It's helpful, thank u

this definitely helped. cheers man

Thanks !!!

Thank you, helped me understand the problem. The question needs to be edited to make it more clear.

definitely it helped a lot :) tnx

thank you .it helps me a lot

Thanks man!

gr8

//wth is wrong with this code?

## include

using namespace std; int main() { int a,b; cin>>a>>b;

}

Try changing "else if(a==9)" to just an else statement because if/else chains always need to end with an else statement to default to if no other case is true.

your code would only work if one of the numbers entered was eight. you should include all numbers less than 9.

That is not true. It is perfectly acceptable to have an if/if else with no last else case if your intention is to have a situation where no statement executes if your specific conditions are not met. It is analogous to writing...

Use this code

8 to 11 "inclusive" is how it should have been originally written. But I guess that's what the square brackets were for.

The square brackets are explained as an interval at the end of the task description :)

Thank you so much for explaining this question... :)

Ain't this wrong?

no its correct

what if the value I give is a=8 and b=5?

You will be given two positive integers, a and b (a<=b), separated by a newline. read the question first .....AH

calm down okay! It was just a mistake. Sorry that I asked you.

Good practice would be to not assume such things, so your intuition is spot on. A simple check at the beginning of the function hurts nothing, as so...

you are right .

its coreect u mother fucker

lmao the comment and reply.. hahaha you guys, made my day

very funny stuff

thanks

Pretty poor solution. Unnecessary use of two for loops. You can do it in one for loop. The code would be easier to read if it was written using one loop and in the loop had an initial if/else structure that determines if the current number in the iteration is less than or equal to 9. If the count is less than 10, you have if statements that print the english word out, and if the count is greater than or equal to 10, then you do modulo to determine even or odd.

it is totally worng answer

It is kind of poorly written, but should give the right results. It can certainly be done all in one for loop though, and in my opinion, using a string array is much cleaner than using a switch statement.

using a double for loop is not necessary

This is wrong. When I Try imput more then 10 eg. 23 and 25 I get to many even and odds.

Try this

I see this kind of solution in many cases. But in fact this is wrong. This works only in case the first input is less than the second one. If the test case would be 12 and 11 then this failed the test case because the code does not enter into the for loop, because c is greater than b.

bro just skill the break statement in switch case and the you will be needing only second loop

but what you will do when both numbers are between 1 and 9...because two given value may be anything,,,may be both are greater than 9 than what?

if two given numbers are between 1 and 9...

like: 2 4

then output: two three four

and if two numbers are greater than 9...

like: 20 23

then output: even odd even

try this with some standard input samples,,,,it may be more comfortable,,,,MAY BE

for 20 23 output: even odd even odd

yess

Thx A lot!!!!

not at all.,,,just remember me in ur wish...:)

You the real MVP!

Thanks..

thank you so much

Thank you very much for help understanding the original question.

Thank you!

i have found that a significant number of problems, need a LOT better definition. the sample/input confuses the normal interpretation of the question.

i have found that a significant number of problems, need a LOT better definition. the sample/input confuses the normal interpretation of the question.

now i understand why this doo hickey problem was asked before having discussed arrays and; why it needs a loop!!!!!

thank you very much

thank you... not helps someone...its helping a lot of people...

Very helpful! saved a lot of time ,Thanks :)

Thanks , earlier the expected output looks bizzare to me. But now , I got the problem.

Thanks

i m still a bit confused... if (no<=9) the text version of it must be got so if input is 8.. we got to get eight.. thats alrite but after that the condition goes like if(no>9) then print even or odd respective of the no.. so for input 11 we must print it as odd only right? the output must be like "eight odd" only no, how does the ouput cums like "eight nine, even,odd" ???

Yes, if the number is less or equal than 9, then print the English representation of it. If it's bigger, you should print "even" or "odd", depending on the number. Now, your input contains two numbers. You have to print every number that is between those numbers, including those two numbers. For example, input is 8 and 11 - you print:

eight

nine

even

odd

If the input is 2 and 6, you should print:

two

three

four

five

six

thanks a lot for ur kind response... got figured out in a very clear way !!

Yup!... It really works.......

I'm glad i looked at the discussion here. It took me all of 15 seconds to fix my code after reading this. The question is clear as mud.

Thanks..it helps me a lot.

thnx a lot

this assignment was not clear at all, what they should say is print from the first given number to 9 in the form of "one", "two" ... "nine", and "even" , "odd", "even", to the second given number.

Question is not that clear. This got me in the right track. :)

Great explanation of the problem, I wasn't understanding the input until now.

Thank you so much!!

Thanks, it helped me

Thanks, I didn't understand what I was messing up till I read your explination.

thanq soo much

thanks

Thank you very much, It's help a lot.

This really

helpedin explanation of the orignal question.Thanks. It's a really badly worded question. It's very easy when explained clearly as you've done.

someone please told them to correct the question

Thanks for this.

Thanks! The comment really helped.

Oh my gosh thank you!

thanks a lot

Thank you !

Horribly worded problem

Thank you a lot. It really helpful.

Thanks . you saved me .

really helpful....thanks bro.

Thank you jvinniec Your suggestion helped me to solve this problem thank you very much

Thanks, I was really annoyed.

thanks bro that was really helpful..

dont know what to say just one word thnxs a lot

yeah it really helped.

thanks bro

thanks bro

Helped alot, Thanks.

Thank you. That helps a lot.

thats good but not great we can have other simple alternatives

thanks so much, i was just lost in the problem. I was just don't getting the question.

Thank you so much. You clarified the question. :)

you actually saves my time thanks....

Thank you.

Thanks a lot sir

it helped a lot man.Thank you

Thanks, couldn't figure it out till I saw your comment. Really appreciate it. Helps us beginners alot.

Wow, the instructions don't say that at all.

Thank you so much. I was sitting with this for 2 hours and never understood what this was about. Thanks.

thanku....it really helped in understanding what they asked for!!

Haha thanks!, this question was very poorly written.

Yes it does! The way these questions are put is often more complex than the code needed lol.

thankyou.

thank you now i got it

Thanks, I was not understanding the question

tq so much,, this cleared all my doubts

really thanks.

Thank you! It really helped.

Thanks alotttt ......

thanks bud

thx

Thank you, now I see what they wanted.

thank you so much. I was confused, it really really helped.

Thank you sir! You are the man! Was getting bothered until I noticed I misunderstood what was meant in the problem. Thank you!!!!

Thnk you it really helped

Thanks a bunch! Strangely worded questions are ... frustrating

Thank you so much! I feel as though this test could have been written a bit clearer.

thnks really helped!!!

Yeah,this is very helpfull to understand the problem.

it is very helpful

tqsm

Finally this is making sense, couldn't figure out why it would need a for loop but in that case it does make sense. thank you!

Thank you!

Thanks, I really couldn't get what they are talking about here.

Thank you so much.

thanx bro...ur clarification really helped..

thank you so much.