# Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +… + 49*49 + 50*50 into total. Use no variables other than k and total.

1

### CHALLENGE:

Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +… + 49*49 + 50*50 into total. Use no variables other than k and total.

### SOLUTION:

```
k = 1;
total = 0;
while ( k <= 50 ){
total = total + ( k * k );
k = k + 1;
}
```

#### One comment

• Phil says:

This solution is accepted on mathlab HOWEVER
this would leave you with the wrong answers if you print it.

total = k*k
total + (k+k) gives you 1 more than the answer.
for exam 2^2 would equal 5 instead of 4.

this is what i did to get the first 50 squared numbers.

public class Whileegs {

public static void main(String[] args) {

int k=0;
int total=0;
while (k<50){

total= (k*k);
System.out.println(total);
k++;

}
}
}

I love this site. thanks, this has been a lifesaver!