0

sum of odd numbers

how to print sum of first 50 odd numbers using do while loop in c

15th Apr 2018, 2:55 PM
Jordan
Jordan  - avatar
9 Réponses
0
dont know c syntax, but in cs it would be like this: int num =1; int count = 0 int sum = 0; do { sum+=num; num+=2; count++; } while(count<50);
15th Apr 2018, 3:02 PM
Sergey Semendyaev
Sergey Semendyaev - avatar
0
bcz ive question in my assignment...
15th Apr 2018, 3:02 PM
Jordan
Jordan  - avatar
0
Sergey Semendyaev If the numbers are increased two by two, the counter must be half (count<25) =)
15th Apr 2018, 8:43 PM
Luciano Ariel Caputi
Luciano Ariel Caputi - avatar
0
No, Luciano, if count<25, you'll get sum of first 25 odd numbers. Each number increases count in my logic.
15th Apr 2018, 9:49 PM
Sergey Semendyaev
Sergey Semendyaev - avatar
0
Sergey Semendyaev Better than explaining with words, is letting the code speak for itself. Successes and see you! =) https://code.sololearn.com/ctH4skgBtW2G/?ref=app
16th Apr 2018, 5:24 AM
Luciano Ariel Caputi
Luciano Ariel Caputi - avatar
0
Luciano, look at your own code's output and count how many numbers does it have)) (if you still didn't get what I mean the 50th odd number is 99)
16th Apr 2018, 5:36 AM
Sergey Semendyaev
Sergey Semendyaev - avatar
0
Sergey Semendyaev First, it's not my code, just add a line to the code you posted above. Basically "num" is increased by two for each interaction of the loop. In this case the amount is reached in half the time (interactions). "Count" represents the number of interactions (50), not the first 50 numbers. I'm sorry if you do not understand well, my English is not good. =)
16th Apr 2018, 6:35 AM
Luciano Ariel Caputi
Luciano Ariel Caputi - avatar
0
But each iteration adds 1 number to the sum, so if you need sum of 50 numbers there should be 49 iterations, but if you need sum of odd numbers from 1 to 50 than code should be different. So it depends on the task and my code sums first 50 odd numbers as was asked in initial post. Peace)✌
16th Apr 2018, 6:49 AM
Sergey Semendyaev
Sergey Semendyaev - avatar
0
As I said, my English is not very good and I interpret odd numbers 1 to 50. See you =)
16th Apr 2018, 7:21 AM
Luciano Ariel Caputi
Luciano Ariel Caputi - avatar