c++ (nested For statements) Help~

Oct 29, 2012 at 2:35pm
Write a program with nested for statements to display the output as follows:
Sum of square of numbers
1x1 = 1
1x1 + 2x2 = 5
1x1 + 2x2 + 3x3 = 14
. . .
1x1 + 2x2 + 3x3 + . . . 9x9 = 285
Oct 29, 2012 at 2:40pm
this doesnt need nested loops.
What is your specific difficulty/problem? Are you stuck somewhere?
Oct 29, 2012 at 2:45pm
i want to display the output as follows:

Sum of square of numbers
1x1 = 1
1x1 + 2x2 = 5
1x1 + 2x2 + 3x3 = 14
. . .
1x1 + 2x2 + 3x3 + . . . 9x9 = 285

The problem is : Must use nested For loop to solve this question
Oct 29, 2012 at 2:54pm
We don't do homework assignments.
Make an attempt at writing the code and post it (using code tags). We'll try and help.
Oct 29, 2012 at 3:01pm
The pattern of output must same like the following and must use 'nested for loop'


1x1 = 1
1x1 + 2x2 = 5
1x1 + 2x2 + 3x3 = 14
1x1 + 2x2 + 3x3 + 4x4 = 30
1x1 + 2x2 + 3x3 + 4x4 + 5x5 = 55
1x1 + 2x2 + 3x3 + 4x4 + 5x5 +6x6 = 91
1x1 + 2x2 + 3x3 + 4x4 + 5x5 +6x6 + 7x7 = 140
1x1 + 2x2 + 3x3 + 4x4 + 5x5 +6x6 + 7x7 + 8x8 = 204
1x1 + 2x2 + 3x3 + 4x4 + 5x5 +6x6 + 7x7 + 8x8 + 9x9 = 285

(just like a right angle triangle shape)
Oct 29, 2012 at 3:06pm
Using the following code, i able to display the sum but i dont know how to display the pattern of the output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#include <iostream>
using namespace std;
void main()
{
	int i=0, j=0, sum;
	for (i=1;i<=9;i++)
	{
		sum=0;

		for (j=1;j<=i;j++)
		{
			sum=sum+(j*j);
			if(j>=i)
				cout<<sum<<endl;
		}
	}

	system("pause");
}
Last edited on Oct 29, 2012 at 3:17pm
Oct 29, 2012 at 3:17pm
Move cout<<sum<<endl; out of the inner loop. and change it to cout << " = "<< sum<<endl;

Replace cout in inner loop with something like cout << j << 'x' << j << " + ";

You will have an additional "+" printed, try to solve that, else post back
Last edited on Oct 29, 2012 at 3:18pm
Topic archived. No new replies allowed.