6. Write a program that read in a number n and then output the sum of the squares from 1 to n. For example: if n=3, the output should be 14.
Sun Dec 08 2024 11:23:33 GMT+0000 (Coordinated Universal Time)
Saved by
@John_Almer
package sumofsquares;
import java.util.Scanner;
public class SumOfSquares {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * i;
}
System.out.println("Sum of squares: " + sum);
}
}
content_copyCOPY
Comments