0

Odd Number exercise in Loops lesson

Take number, starting at 1 give odd numbers until number is reached. Code not working! Help! https://code.sololearn.com/cfKlX5Tj4NhO/?ref=app

16th May 2023, 12:12 AM
Artemis18x
Artemis18x - avatar
5 Answers
+ 3
Minor error: should be i+=2, not just i+2, or else i never actually gets increased. That side, you've put in the conditions the "until" for when you want your for loop to end, but it takes the conditions "while" it's supposed to continue. So as written, if num is larger than 2, it'll never run... But if num is 2 or smaller, it'll run forever! Switch to <= there and you should be good to go. Side note, that's gonna be equivalent to i<num if you'd like to simplify. Also, make sure you're supposed to exclude num, since that's what you're currently doing.
16th May 2023, 12:31 AM
Orin Cook
Orin Cook - avatar
+ 2
Oh, one more potential source of test problems: you've included a space in your output, but since writeLine goes to the next line after it's done, you probably don't want the space
16th May 2023, 12:33 AM
Orin Cook
Orin Cook - avatar
+ 2
Thanks so much! That damn = was messing me up. And switching to < fixed it all! 💯
16th May 2023, 12:39 AM
Artemis18x
Artemis18x - avatar
0
Artemis18x maybe this? for(int i=1;i<=num;i+=2)
16th May 2023, 9:03 AM
Bob_Li
Bob_Li - avatar
0
Ended up using for(int i =1;i-1<number;i+=2)
10th Jun 2023, 1:37 AM
Artemis18x
Artemis18x - avatar