why is my code displaying values twice in a row? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why is my code displaying values twice in a row?

// This code should display the square values of each odd integer within the range 1-13 inclusive. It should then add them togethger and output the final value which is 455. // For some reason, it is displaying the square numbers twice. This is causing the final value to be incorrect. //What have I tried? // oddInt *= oddInt in the for statement with oddInt++ in various places (after each var declaration or cw). // oddInt++ in the for statement. This way does output the correct suare numbers. However it displays them twice in a row so that the final value is 741. // namespace SoloLearn { class Program { static void Main(string[] args) { int sqrOddInt = 0; int oddIntTotal = 0; for (int oddInt = 1; oddInt <= 13; oddInt*= oddInt) { if (oddInt % 2 != 0) sqrOddInt = oddInt * oddInt; Console.WriteLine(sqrOddInt); oddIntTotal += sqrOddInt; } Console.WriteLine(); Console.WriteLine(oddIntTotal); } } }

11th Aug 2019, 12:47 PM
Emma
4 Answers
+ 1
Found this solution for you. https://code.sololearn.com/cSb14aJR9YGg/?ref=app solved it by adding an "else", as you had an "if". you should specify what the program should or shouldn't do if the condition is not met. also didn't know what the empty write line was for, so i removed it.
11th Aug 2019, 3:30 PM
storm
storm - avatar
+ 1
You're welcome Emma . am not a c# pro, but if you know one language, you find your way around in the other languages too :)
11th Aug 2019, 3:49 PM
storm
storm - avatar
0
Thanks 'H A P PY T O H E L P, 🙂
11th Aug 2019, 3:37 PM
Emma
0
Thanks storm. I like that the running total is shown. The empty WriteLine was there to space the total from the sqr ints🙂
11th Aug 2019, 3:46 PM
Emma