Finding The "Counter" Total of A Loop

*
Last edited on
remove line 34 total should +=num but dont reset it to = num. Let it just accumulate value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//This program uses a While loop
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{ 
	int num=1;
	int square=0; 
	int counter=30;
	int total=0; 

	//Loop 1-30 
//	num = 1; 
//	counter = 30; 

cout << " Num" << "\tSquare" << "\tTotal" << endl;

while (num <= counter) 
{
	//Output 1st column 
	cout << setw(4) << num;
    
	//Squared Calculation
	square = num*num;

	total=total+num;	
                
	//Output 2nd column
	cout << setw(6) << square << "\t" << total<< endl;     
	num++;

}

return 0;
}
Topic archived. No new replies allowed.