doing an assignment for my computer science class and also can"t quite number out what I"m act wrong. I need to write a routine that adds every the squares in between 1 and 100 (1, 4, 9, 16, 25, 36, 49, 64, 81, 100) As component of the assignment I"m forced to use a "while" statement, therefore no "for" statements will help. My existing code is as follows.

import java.util.Scanner;public class While42B { public static void main(String <>args ) { Scanner reader = brand-new Scanner(System.in); int n = 1; int amount = 0; while (n and the return I gain from the GUI is

710

Any assist would be significantly appreciated, say thanks to you!




You are watching: The sum of all squares between 1 and 100

*

*

Look at this statement:

n = (n*n);You"re squaring n within the loop, and then incrementing it. Do the following instead:

while (n This way, you don"t change n through squaring it, and you can properly monitor its value for the while loop.


*

I establish you"re looking for a when loop butjust FYI you can use the straight formula:

System.out.println( n * (n + 1) * (2 * n + 1) / 6);


*

Just together an add-on, in Java 8, one can do the sum of squares of an initial 10 herbal numbers together follows:

int amount = IntStream.rangeClosed(1, 10).map(n -> n * n).sum();

*

If you want to do it in your way I mean very first square the value of each number,keep the in a variable and add it come the sum you deserve to use a different variable instead of n to save the square value of every number favor this :

int n = 1;int squareValue;int sum = 0;while (n
You are changing the value of n in: n= n*nSo now you are not looping native 1 to 100, you are skipping a many numbers




See more: Leonardo Dicaprio Happy Walk In' Ideas, Strutting Leo

Simply review the mathematics and then continue to translate the math operations into code.I"ve worked out the mathematics which ns hope could assist you. I have actually also provided an answer come the question proclaimed by her professor, I perform hope that i am may be to have actually been helpful in her arrangements. No credit needed except to Carl Friedrich Gauss.

X(nsquared base 1 + nsquared base n) split by 2

X equals number of numbers (100) and n basic 1 equals very first number (1) and also n basic n equates to last number (100), i did not include the squares in the number description however you do require too include squares in the an initial and critical number. 1 squared and 100 squared.

Do these operations according as well PEMDAS ( stimulate of operations ).I don"t have a high sufficient reputation so ns can"t article photos, sorry for that, ns hope this all assisted though.The answer is (500,050)