While Loop Sum of Squares

*CLOSED*
Last edited on
Trace your program. Imagine what will hapeen on each loop itetration.
like:

"I enter 5 for i, so before loop values are: counter = 1, i = 5, ..."
"Then I enter loop, counter (1) is less than i (5) so loop is executed"
"I assign i*i (5*5) to product, then I add product (25) to total, now it is 25"
"I increment counter, now it 2"
"Next iteration, counter (2) is less than i (5), so loop continues"
Thank you for answering.
I did trace & I think what I wrote is correct.
But the output is wrong so there must be something I'm missing that I cant figure out yet.
Do you have any idea what's wrong with my code?
Thanks again.
Lets trace it together then:
"I enter 5 for i, so before loop values are: counter = 1, i = 5, ..."
"Then I enter loop, counter (1) is less than i (5) so loop is executed"
"I assign i*i (5*5) to product, then I add product (25) to total, now it is 25"
"I increment counter, now it 2"
"Next iteration, counter (2) is less than i (5), so loop continues" 
"I assign i*i (5*5) to product, then I add product (25) to total, now it is 50"
"I increment counter, now it 3"
"Next iteration, counter (3) is less than i (5), so loop continues" 
"I assign i*i (5*5) to product, then I add product (25) to total, now it is 75"
"I increment counter, now it 4"
"Next iteration, counter (4) is less than i (5), so loop continues" 
"I assign i*i (5*5) to product, then I add product (25) to total, now it is 100"
"I increment counter, now it 5"
"Next iteration, counter (5) is equalt to i (5), so loop continues" 
"I assign i*i (5*5) to product, then I add product (25) to total, now it is 125"
"I increment counter, now it 6"
"Next iteration, counter (6) is greater than i (5), loop terminates"
"Total is 125"
See? Output matches trace. Maybe you calculated something wrong? Like wrote wrong variable somewhere? I highligted part you should pay attention to. See what is wrong with it?
Write a program that uses while loop to compute and prints the sum of a given number of squares. For example, if number 5 is entered, then the program will print 55, which equals 1^2 + 2^2 + 3^2 + 4^2 + 5^2.


does anybody can answer this question?
@shay00 How's that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using std::cout; using std::cin; using std::endl;

int main() 
{
	int n = 0, sum = 0;
	cout << "Enter a number: ";
	cin >> n;
	
	while(n > 0)
	{
		sum += n * n;
		n--;
	}
		
	cout << "\nSum: " << sum << endl;
	
	return 0;
}
Last edited on
@integralfx
Thank you it works properly
hi and sorry
how this program work ?
for example
i enter 3 then sum 14
thanks
Last edited on
You enter in a number, let's say 3.
sum = 0, n = 3
The program squares the number (3 * 3) and adds it to sum.
It then decrements n.
sum = 9, n = 2
It squares 2 (2 * 2) and adds it to sum.
It then decrements n.
sum = 13, n = 1
Finally, it squares 1 (1 * 1) and adds it to sum.
sum = 14, n = 0
Because the while loop only stays true when n is greater than 0 (0 isn't greater than 0, it's equal to 0, which makes the while loop false), the loops stops and outputs sum.

Topic archived. No new replies allowed.