0
sum of odd numbers
how to print sum of first 50 odd numbers using do while loop in c
9 Answers
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);
0
bcz ive question in my assignment...
0
Sergey Semendyaev
If the numbers are increased two by two, the counter must be half (count<25) =)
0
No, Luciano, if count<25, you'll get sum of first 25 odd numbers. Each number increases count in my logic.
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
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)
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. =)
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)â
0
As I said, my English is not very good and I interpret odd numbers 1 to 50. See you =)