• Register
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);
    }

Thanks for your positiveness and effort. I must recognise your help.

15.3k points
reopened by

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

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.

Please follow the snippet below:

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

 I hope you get my points. If you still have any queries, please reply to this thread, and if you get your answer leave an upvote here.

Thanks.

94.3k points
selected by

Related questions

0 votes
1 answer 3 views
3 views
Problem: The sum of all squares between 1 and 100?
asked 5 days ago Mezbah 110k points
0 votes
1 answer 5 views
5 views
Problem: The sum of all squares between 1 and 100 (inclusive).
asked Jun 26 akhi 106k points
0 votes
1 answer 15 views
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
asked Apr 25 muktaa 34.6k points
0 votes
1 answer 19 views
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:?
asked Jun 10 Mezbah 110k points
0 votes
1 answer 238 views
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!
asked Feb 25 Code Learner 10.5k points
0 votes
1 answer 47 views
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...
asked May 4 anika11 32.2k points
1 vote
1 answer 547 views
547 views
Problem: How to find the sum? C++ program to find sum of n numbers using while loop.
asked Apr 18 PkGuy 25.9k points
0 votes
1 answer 10 views
10 views
Problem: Write a program to print even and odd numbers in PHP?
asked Jun 26 Nahil 107k points
0 votes
1 answer 14 views
14 views
Problem: Write a while loop that lets the user enter a number?
asked Jun 26 Nahil 107k points
0 votes
1 answer 104 views
104 views
Problem: Hi there! I am working with While loop in C++ and I want to write a program that continues to bid unless the user enters &lsquo;n&rsquo; or &lsquo;N&rsquo;. I understand the while loop but unable to make the logic. Please provide me the code with detailed comments. Thank You!
asked Feb 25 Code Learner 10.5k points