# Write a program that finds the sum of all squares between 1 and 100

1 vote
199 views

## Problem:

I have got an assignment from my online java class and doing the assignment and can't quite figure out what I'm doing wrong. I have to write a program that finds the sum of all squares between 1 and 100. As part of the assignment, I'm required to use a "while" statement, so no "for" statements will help. My current code is as follows below:

``````import java.util.Scanner;
public class SquareClass {
public static void main(String []args ) {
Scanner sc = new Scanner(System.in);
int a = 1;
int sum = 0;
while (a <= 100) {
a = (a*a);
a++;
sum = (sum + a);
}
System.out.println(sum);
}``````

reopened

1 vote

## Solution:

You have mistaken it twice in your code. Firstly, you don't need a Scanner class. You are not gonna prompt a user. So, remove that. Secondly, if you do this in your way, you need to declare another variable to keep all the squared values added in it.

``````int a = 1;
int squareValue;
int sum = 0;
while (a <= 100) {
squareValue= (a*a);
sum += squareValue;
a++;
}
System.out.println(sum);``````

Thanks.

94.3k points
selected by

## Related questions

3 views
Problem: The sum of all squares between 1 and 100?
5 views
Problem: The sum of all squares between 1 and 100 (inclusive).
15 views
Problem: I've tried multiple different ways but all are infinite loops or recursions: i = 1 user_num = int(input()) while user_num > 1: print(user_num - (user_num - 1)) Sample output with input: 4 1 2 3 4
19 views
Problem: I'm not sure how I came across this : Write code that prints: usernum ... 2 1 print a newline after each number. ex: usernum = 3 outputs:?
238 views
Problem: I have seen a program that does so using the for-loop. I want the pseudo-code to find the sum of all numbers divisible by 4 using a while loop. Thanks!
47 views
Problem: I am trying to write as the question is stated, Write a program that accepts a positive integer from the user and print the first four multiples of that integer; Use while loop (Python) total = 0 number = int(input("Enter integer: ")) while number <= 15: total = total + ... Enter integer: 5 0 5 10 15 this is the example my professor wanted This is what i have so far, but i'm a bit lost...
1 vote
547 views
Problem: How to find the sum? C++ program to find sum of n numbers using while loop.