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

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.

13 9 6
94,240 points

Related questions

0 votes
1 answer 28 views
28 views
Problem: any help : Write a program that squares an integer and prints the result..
asked Mar 6 Wafa Abu Yousef 6.1k points
3 votes
1 answer 3.9K views
3.9K 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, 2020 Gavin 15.3k points
0 votes
1 answer 36 views
36 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 9.2k points
0 votes
1 answer 40 views
40 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
asked Jan 21 Code Learner 9.2k points
0 votes
1 answer 37 views
37 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 9.2k points
0 votes
1 answer 101 views
101 views
Problem: Write a shell script to print numbers as 5 4 3 2 1 using while loop.
asked Feb 13 Shovo210 13.2k points
1 vote
1 answer 860 views
860 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, 2020 samhaz 5k points
1 vote
1 answer 1 view
1 view
Problem: I am stuck in this problem during my learning please help. Java program to find prime number between 1 and 100.
asked Mar 24 PkGuy 13.1k points
0 votes
1 answer 3 views
3 views
Problem: What is the difference between while and do while java?
asked Mar 14 Ethan ross 2.3k points
0 votes
1 answer 6 views
6 views
Problem: I saw a statement somewhere that said while (1). I did not understand what this statement does and why do we use it? Can anybody here please explain the purpose of this statement to me, please?
asked Mar 2 Code Learner 9.2k points