Solved: Write a java program that inputs an integer greater than 0 and calculates the sum of the squares of the numbers from 1 to that integer.

Write a java program that inputs an integer greater than 0 and calculates the sum of the squares of the numbers from 1 to that integer. For example, if the integer equals 4, the sum of the squares is 30 (1 + 4 + 9 + 16). The program should repeat this process until the user wants to quit. An input value less than or equal to 0 signals the end of the data. Note: one way to calculate this would be to use the formula n(n+1)/2. You must write this program without using that formula.

Expert Answer

 

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Bhava
{
public static void main (String[] args) throws java.lang.Exception
{
int ques;
int ans;
Scanner input = new Scanner (System.in);
System.out.println(“Enter a number greater than 0 (less than 1 to quit):”);
ques = input.nextInt();
while (ques >= 1)
{
ans = 0;
for (int i = 1; i <= ques; i++)
{
ans += i * i;
}

System.out.println(“The sum of the squares from 1 to “+ques + ” is ” +ans);
System.out.println(“Enter a number greater than 0 (less than 1 to quit):”);
ques = input.nextInt();
}
}
}

Output
*******
Enter a number greater than 0 (less than 1 to quit): 9
The sum of the squares from 1 to 9 is 285
Enter a number greater than 0 (less than 1 to quit):

Still stressed from student homework?
Get quality assistance from academic writers!