- Practice
- C++
- Introduction
- For Loop
- Discussions

# For Loop

# For Loop

jvinniec + 99 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.

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

Abhi1428 + 1 comment Thanx,it really helped..

cs15m059 + 1 comment thanks,it really Helped more

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

16131a05e3cse3 + 0 comments thank you,its been helpful

CHODHARY + 7 comments # 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 {`if(a%2==0) cout<<"even\n"; else cout<<"odd\n"; }`

} return 0; }

liordah01 + 4 comments 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;

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

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

shuklarituraj9 + 16 comments This is my code

#include <iostream> #include <cstdio> using namespace std; int main() { int i,c,b; string a[11]={"even","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<<a[0]<<endl; else if((i>9) && (i%2!=0)) cout<<a[1]<<endl; else cout<<a[i+1]<<endl; } return 0; }

Headhunter1 + 0 comments nice bruh!

gajendra14695 + 2 comments in your code why are u useing the variable c,b;

sreehema98 + 0 comments b and c are variables to take 2 inputs.

shubham_maske24 + 0 comments actually its use for take input.

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

avinashak558 + 0 comments What if b>c?? it wont work for b>c test case

balakumaran428 + 0 comments that's great man, you have clarified my doubt. thanks a lot

420vijay47 + 0 comments thnkx

s1035111590 + 0 comments perfect....!

abhileshgupta74 + 0 comments nice code

souravrawat1999 + 0 comments thanks bro u cracked it

prakruthiks3 + 0 comments 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"<Louiealdana + 0 comments sweet

Louiealdana + 0 comments sweet

mknekong + 0 comments the initializer for string didn't work out with me

Aminson_Jets + 0 comments Nice work!

Afzar + 0 comments Nice

blackwxf + 0 comments [deleted]

vvyy346 + 0 comments 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

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

pradyumnachak98 + 0 comments same here

srikanthhubli + 0 comments c dosnt hace cin u should use scanf

bergart11 + 8 comments Great code, I like it.

Here is mine:

#include <iostream> #include <cstdio> using namespace std; int main() { // Complete the code. int a,b; int n=0; string intMap[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; } } else if ((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; } } } return 0; }

piyushp0541 + 4 comments for(n=10;n<=b;n++){

if (n%2==0){ cout<<"even"<`what these lines are performing can anyone explain please ....`

Vexify + 2 comments //Compares the values of a, b with 9 and if values <= 9 then //Prints all values from 'a' uptill 'b' in string format if ((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.... else if ((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 them for(n=10;n<=b;n++){ if (n%2==0){ cout<<"even"<<endl; } else { cout<<"odd"<<endl; } } } Hope that helps.

samarthchadda_i1 + 0 comments why you have used intmap[n-1]

fatursetiawan80 + 0 comments thank you so much...

sanjay_kanakkot + 0 comments [deleted]sanjay_kanakkot + 0 comments 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..

klausskent17 + 0 comments 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 :-)

eshank_tiwari + 0 comments [deleted]abhisheksud96 + 0 comments what is the use of String intMap[]

vasu71051 + 0 comments 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?

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

V0dK6 + 0 comments 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 ;-)

shuklarituraj9 + 2 comments Much simple, try this.

#include <iostream> #include <cstdio> using namespace std; int main() { int i,c,b; string a[11]={"even","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<<a[0]<<endl; else if((i>9) && (i%2!=0)) cout<<a[1]<<endl; else cout<<a[i+1]<<endl; } return 0; }

annufranci + 0 comments [deleted]AuCcH + 0 comments it helped

codertoaster + 1 comment Unnecessarily making it so long and unreadable.

string words[]={"one","two","three","four","five","six","seven","eight","nine"}; for(int n=a;n<=b;n++) { if(n>9) if(n%2==0) printf("even\n"); else printf("odd\n"); else printf("%s\n",words[n-1].c_str()); }

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

cdabbott + 0 comments 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

rvinothkanna + 0 comments Thanks a lot

keh_mario + 0 comments Don't waste your time typing all that out

int main() { // Complete the code. int a, b; cin>>a>>b; string num[9] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; for(int i=a; i<=b ;i++) { if(i<=9) cout<<""<

cjschuller01 + 0 comments you could also use an arry insted on usong all these lines

rattikarn_dudley + 0 comments Thanks! this helped a lot.

Adil + 0 comments Thank you!

akash4u + 0 comments thank you...

redpix_ + 0 comments [deleted]alextaylor + 1 comment I could not make sense of the original question as written. Thank you.

bigdan4712 + 0 comments 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.

hugo_lin + 0 comments It's helpful, thank u

OrionH + 0 comments this definitely helped. cheers man

NapdaN + 0 comments Thanks !!!

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

shemul + 0 comments [deleted]souravsam + 0 comments definitely it helped a lot :) tnx

geo_hackerrank + 0 comments thank you .it helps me a lot

furiacs + 0 comments Thanks man!

trailblazerr + 0 comments gr8

ajit4all21 + 3 comments //wth is wrong with this code?

# 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%2==0) cout<<"even\n"; else cout<<"odd\n"; } } return 0;`

}

xsmgxbrayley + 2 comments 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.

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

drkato + 0 comments 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...

else ;

maximshen + 0 comments [deleted]shuklarituraj9 + 0 comments Use this code

#include <iostream> #include <cstdio> using namespace std; int main() { int i,c,b; string a[11]={"even","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<<a[0]<<endl; else if((i>9) && (i%2!=0)) cout<<a[1]<<endl; else cout<<a[i+1]<<endl; } return 0; }

Khushboo_Watwani + 1 comment Thank you so much for explaining this question... :)

akhii + 10 comments int a,b,i; cin>>a; cin>>b; for(i=a;i<=b;i++) { switch(i) { case 1: cout<<"one \n";break; case 2: cout<<"two \n";break; case 3: cout<<"three \n";break; case 4: cout<<"four \n";break; case 5: cout<<"five \n";break; case 6: cout<<"six \n";break; case 7: cout<<"seven \n";break; case 8: cout<<"eight \n";break; case 9: cout<<"nine \n";break; } } for(i=10;i<=b;i++) { if((i%2)==0) cout<<"even \n"; else cout<<"odd \n"; }

decoder_ + 1 comment Ain't this wrong?

akhii + 1 comment no its correct

decoder_ + 1 comment what if the value I give is a=8 and b=5?

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

decoder_ + 1 comment calm down okay! It was just a mistake. Sorry that I asked you.

drkato + 1 comment 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...

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

RA1511008010245 + 0 comments you are right .

shahashish991 + 2 comments [deleted]akhii + 0 comments its coreect u mother fucker

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

b6032682 + 0 comments very funny stuff

deekshith565 + 0 comments [deleted]shajol484 + 0 comments thanks

samplenhold + 0 comments 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.

alice_ferrazzi + 0 comments using a double for loop is not necessary

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

shuklarituraj9 + 1 comment Try this

haberpeter + 0 comments 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.

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

Meenachinmay + 1 comment 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?

Khushboo_Watwani + 3 comments 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

Meenachinmay + 0 comments try this with some standard input samples,,,,it may be more comfortable,,,,MAY BE

mukulpahwa504 + 0 comments for 20 23 output: even odd even odd

bindass844 + 0 comments yess

GrimNight + 1 comment Thx A lot!!!!

Meenachinmay + 0 comments not at all.,,,just remember me in ur wish...:)

Tushar_Mehr + 0 comments You the real MVP!

rushabhajain4444 + 0 comments Thanks..

dynamicsahithi + 0 comments thank you so much

dulini88 + 0 comments Thank you very much for help understanding the original question.

SEBPort0 + 0 comments Thank you!

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

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

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

jingren1021 + 0 comments thank you very much

SidPrajosh + 0 comments thank you... not helps someone...its helping a lot of people...

iamrizubiswas + 0 comments Very helpful! saved a lot of time ,Thanks :)

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

xgasko + 0 comments Thanks

hacktik + 1 comment 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" ???

xgasko + 1 comment 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

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

Abul_Kalam + 0 comments Yup!... It really works.......

ascotan + 0 comments 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.

EtsRk + 0 comments Thanks..it helps me a lot.

Sivakumarramar + 0 comments thnx a lot

ricargu2010 + 0 comments 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.

smokhele48 + 0 comments Question is not that clear. This got me in the right track. :)

menglish2451 + 0 comments Great explanation of the problem, I wasn't understanding the input until now.

rheasanjan + 0 comments Thank you so much!!

Parashuram_Joshi + 0 comments Thanks, it helped me

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

surajsetty_sbi + 0 comments thanq soo much

ojas_k30 + 0 comments thanks

h754548560 + 0 comments Thank you very much, It's help a lot.

kashif_i + 0 comments This really

**helped**in explanation of the orignal question.m_patterson + 0 comments Thanks. It's a really badly worded question. It's very easy when explained clearly as you've done.

muhammadburhanz1 + 0 comments someone please told them to correct the question

vatsal09 + 0 comments Thanks for this.

naiktanvi + 0 comments Thanks! The comment really helped.

laurap + 0 comments Oh my gosh thank you!

stayx + 0 comments thanks a lot

ssanck + 0 comments Thank you !

fireash + 0 comments Horribly worded problem

caitenkhongbitr1 + 0 comments Thank you a lot. It really helpful.

zwlady253 + 0 comments Thanks . you saved me .

Engineer_x + 0 comments really helpful....thanks bro.

maheshvangala191 + 0 comments Thank you jvinniec Your suggestion helped me to solve this problem thank you very much

kai_abhik + 0 comments Thanks, I was really annoyed.

ABInfinity + 0 comments thanks bro that was really helpful..

vishalshukla044 + 0 comments dont know what to say just one word thnxs a lot

snmahajan30 + 0 comments yeah it really helped.

naveensurya3010 + 0 comments thanks bro

naveensurya3010 + 0 comments thanks bro

paraschopra2000 + 0 comments Helped alot, Thanks.

chancs88 + 0 comments Thank you. That helps a lot.

samarthchadda_i1 + 0 comments thats good but not great we can have other simple alternatives

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

vishariaraj + 0 comments Thank you so much. You clarified the question. :)

shreyanshgoyal90 + 0 comments you actually saves my time thanks....

ketansuthar899 + 0 comments Thank you.

zppr12 + 0 comments Thanks a lot sir

Shi_vam11 + 0 comments it helped a lot man.Thank you

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

alexbryant + 0 comments Wow, the instructions don't say that at all.

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

15JG1A0490 + 0 comments thanku....it really helped in understanding what they asked for!!

yvohu2002 + 0 comments Haha thanks!, this question was very poorly written.

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

mkathpal09 + 0 comments thankyou.

mohitkumar200131 + 0 comments thank you now i got it

olavo_o_neto + 0 comments Thanks, I was not understanding the question

ramyaasrih + 0 comments tq so much,, this cleared all my doubts

haniyeh_ghassami + 0 comments really thanks.

vengelke + 0 comments Thank you! It really helped.

tiwarisiddharth3 + 0 comments Thanks alotttt ......

ankur229 + 0 comments thanks bud

teoh_xu_yang + 0 comments thx

Danishta + 0 comments Thank you, now I see what they wanted.

satyaprakashnig1 + 0 comments thank you so much. I was confused, it really really helped.

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

cajay550 + 0 comments Thnk you it really helped

victoribasco18 + 0 comments Thanks a bunch! Strangely worded questions are ... frustrating

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

parthduggal777 + 0 comments thnks really helped!!!

verma_rajesh6615 + 0 comments Yeah,this is very helpfull to understand the problem.

bhavanidinesh_b1 + 0 comments it is very helpful

sushmabegumshai1 + 0 comments tqsm

Dineshs91 + 1 comment Input format and sample input are different. The question is ambiguous.

pavelkushtia + 1 comment Yes, no newline between the inputs. Either the description or the test case input should be corrected.

fogbank + 0 comments Indeed. In testcases 1-5 the numbers are separated by a space. I'm not changing my code, since it's correct for the problem definition. The mistake is in the testcases, not in my code.

rockpile + 5 comments `int a,b; string num[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; cin >> a >> b; for (int i = a; i <= b; ++i) { if (i > 9) { i % 2 == 0 ? cout << "even\n" : cout << "odd\n"; } else { cout << num[i] << endl; } } return 0;`

AffineStructure + 0 comments wow I really enjoy the ternary operator.

Bharath113 + 0 comments [deleted]nhantnguyen233 + 2 comments #include <iostream> #include <cstdio> using namespace std; int main() { // Complete the code. string const number[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int a, b; cin >> a >> b; for (int i = a; i <= b; i++) { if (i > 9) cout << (i % 2 ? "odd" : "even") << endl; else cout << number[i - 1] << endl; } return 0; }

kilianovski + 1 comment wity method ^)

shahashish991 + 1 comment go fuck youtself

decoder_ + 0 comments why you're not worth fucking someone?

kalyannaanu666 + 0 comments [deleted]

ibroheem + 0 comments Test for -ve val (i >=1 and i <=9)

SudhanshuMohan + 0 comments Very simplistic. Good job.

JChiquin + 5 comments The ternary operator is the best!

int main() { string hour[]={"even","odd","one","two","three","four","five","six","seven","eight","nine"}; int a,b; for(cin>>a>>b;a<=b;a++) cout<<hour[a>9?a%2:a+1]<<'\n'; return 0; }

shivanidalmia131 + 0 comments very nice

ngochungnguyen21 + 0 comments very good! tks for share...

ramana_cpp + 0 comments excellent

chancs88 + 0 comments beautiful

agnelpb + 0 comments That's awesome. Thanks.

Manbear + 0 comments This challenge needs editing, because the description is very vague and can only be solved if referring to the comments section for an explanation.

captainhampton + 0 comments The way this question is phrased is awful. This could use quite an update to make it more clear precisely what is required.

peecee + 0 comments Anybody else feel this was a badly framed question?

ramey_steven + 0 comments Garbage question. Needs to be reworded.

polmki99 + 1 comment Ternary operator FTW!

string nums [10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int start, end; cin >> start; cin >> end; for (int i = start; i <= end; i++) { cout << (i < 10 ? nums[i] : i % 2 == 0 ? "even" : "odd") << endl; }

___jaw___ + 0 comments Yours is obviously better.

cin >> start >> end; for(int i = start; i <= end; i++){ (i > 9)?((i%2) == 0)? printf("even\n"):printf("odd\n"):(i > 8) ? printf("nine\n"):(i > 7) ? printf("eight\n"):(i > 6)? printf("seven\n"):(i > 5)? printf("six\n"):(i > 4)? printf("five\n"):(i > 3)? printf("four\n"):(i > 2)? printf("three\n"):(i > 1)? printf("two\n"):printf("one\n"); }

Geoff_Fitz + 0 comments # include

# include

using namespace std;

int main() { int num1,num2; cin >> num1 >> num2; string Arr1[9] = {"one","two","three","four","five","six","seven","eight","nine"}; for(int i =num1; i <= num2;i++) { if(i <= 9) { cout << Arr1[i-1] << endl;

} else { if(i%2 ==0) { cout << "even" << endl;

} else { cout << "odd" << endl;

} } } return 0; }Tried to make it short as possible;

Sort 525 Discussions, By:

Please Login in order to post a comment