• Register
1 vote
22 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.

12 7 7
15,250 points

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.

13 9 6
94,240 points

Related questions

3 votes
1 answer 1.8K views
1.8K views
Problem: Hello good soul, I am a student of basic java programming. I was solving beginner-level problems from there. Right now I got a problem that says, write a program that squares an integer and prints the result. Well, I am not one of those who always look solution for their assignment on StackOverflow or other sites. I just want to learn the process. Thanks for your help.
asked Mar 24 Gavin 15.3k points
1 vote
1 answer 59 views
59 views
Problem: I wrote a program in python that asks the user for a number n and prints the sum of the numbers 1 to n: x = 1, addition = 0 n = int(input('Please enter a number:')) for i in range(x, n): addition = addition + i print (addition) But when ... n): TypeError: 'tuple' object cannot be interpreted as an integer Is there any thing that is missing from my code. Please help me to solve this out.
asked Jun 10 samhaz 5k points
0 votes
1 answer 35 views
35 views
Problem: I am trying to find the solution for below assignment: &ldquo;Write a program to find the sum of following series 1^1+2^2+3^3&rdquo;. So I am looking for someone who can give me the idea of a very efficient algorithm for large n (for example 10^10) to find a sum of the ... ++) t=((long long)t*i)%m; sum=(sum+t)%m; } printf("%d\n",sum); } Kindly help me in my above mentioned assignment.
asked Jul 11 Raphael Pacheco 4.9k points
1 vote
1 answer 189 views
189 views
Problem: Hello all, I am a new, college student who is learning python programming. I am thinking to write a program that will input a text file and read all the vowels from it and determines when it comes to the end of the file. So, is it possible to create a while loop that determines when the end of a file has been reached? Thanks in advance.
asked Mar 16 Gavin 15.3k points
0 votes
1 answer 7 views
7 views
Problem: Hi there! I want my while loop to stop after it reaches a certain point and jump out of the loop. Is there any mechanism that I can use? Please help!
asked Oct 12 Sheeza 2.9k points