# would anybody be kind enough to help?

• 07-07-2004
almich
would anybody be kind enough to help?
I am stuck on a homework problem. What I am suppose to do is write a program that calculates the square of all the intergers from 1 till the number a user enters. So my program should calculate something like 1^2 + 2^2 + ...... n^2. I know I am suppose to use the for loop or the while loop, but I am lost. This is what I came up with so far:

using while loop

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {         int x;         cout << "Please enter a number: ";         cin >> x;         int i = 1;         while (i < x)         {                 x++;                 int sum=i+x;                 cout << "Result: " << pow(sum,2) << endl;         }         return 0; }```
using for loop
Code:

```#include <iostream> #include <cmath> using namespace std; int main() {         int x;         cout << "Please enter a number: ";         cin >> x;         for (int i = 1; i< x+1; x++)                 cout << "The sum of the square of all intergers from                 1 to the number you have entered is: "                 << pow(x=i+x,2) << endl;         return 0; }```

Any help and guidance would be greatly appreciated.
• 07-07-2004
outlast
try:
Code:

``` #include <iostream> #include <cmath> using namespace std; int main(){                 int x=0, sum = 0;         cout << "Please enter a number: ";         cin >> x;                 while (x){                 sum+=pow(x,2);                 --x;         }                 cout << "Result: " << sum <<endl;         return 0; }```
• 07-07-2004
swoopy
I commented your old code so you could see the changes. This prints the calculation each time thru the loop. Once debugged, you may want to move the cout after the loop.
Code:

```#include <iostream> #include <cmath> using namespace std; int main() {         int x;         cout << "Please enter a number: ";         cin >> x;         int sum = 0;         int i = 1;         //while (i < x)         while (i <= x)         {                 //x++;                 //int sum=i+x;                 sum += i * i;                 cout << "Result: " << sum << endl;                 //cout << "Result: " << pow(sum,2) << endl;                 ++i;         }         return 0; }```
You could also use pow() instead of i*i.
• 07-07-2004
prog-bman
here's a really short soultion
Code:

```#include <iostream> using namespace std; int main() {   int x;   cout<<"Enter Number"<<endl;   cin>>x;   int i=1;   while (x >= i)         {                 cout << "Result: " << i*i << endl;                 i++;         }         return 0; }```
• 07-07-2004
swoopy
>here's a really short soultion

Well it is short for sure. :rolleyes:
• 07-07-2004
outlast
yeah, short code is nice! ;)
• 07-07-2004
prog-bman
Whoops i'm dumb I totally read the question wrong :o
• 07-07-2004
almich
wow, thank you all for your help! i am glad i learned something from this board.
• 07-07-2004
prog-bman
Your welcome even though im dumb and come back anytime you have some questions i'll try not to miss read your questions agian :)
• 07-07-2004
raum
I rewrote your for loop for a few reasons. First I would use the FOR loop for this problem over the while loop, since you need to declare, test and count. Second if this is for school, most teachers will know if you go beyond your ability and while what everyone else posted is correct most new programmers don't think that way. Ex: while(x).

Next, you made some bad logical errors in this one. Namely, having the cout in the loop and taking x^2 instead of i^2 the wrong number. I know for loops are the toughest of the 3 main loops but if you take a little time to learn them it's hard to not use them.

Remember the standard for FOR loops is i is the counter variable and increases to a number in this case x. You have to use a sum variable to hold the ongoing result of the for loop unless you want each i square to be output. The FOR loop is only one statement here so the braces are optional. The pow() statement is of the format pow(number, power); No equals or the like are needed. I think that pretty much covers it.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int x;
int sum = 0;
cout << "Please enter a number: ";
cin >> x;
for (int i = 1; i <= x; i++)
sum += pow(i,2);

cout << "The sum of the square of all integers from
1 to the number you have entered is: "
<< sum << endl;
return 0;
}
• 07-07-2004
laserlight
Well, in this case there is an even simpler solution:
Given an integer n, n > 0
1^2 + 2^2 + 3^2 + ... + n^2 = n(n+1)(2n+1)/6

By implementing this formula, you would avoid having to use a loop.

On the other hand, this is probably what you would use in a 'real' program, not a school assignment where the teacher wants you would learn how to use loops better by writing a program.

EDIT:
in this case, do not use pow() as it would be overkill.