7-2 sdut-JAVA-Sum Of Squares
分数 8
全屏浏览
切换布局
作者 马新娟
单位 山东理工大学
Write a Java application program to compute and print the sum of a given number of squares. For example, if 5 is input, then the program will print 55, which equals
You are required to write your solution using a for loop.
Input Specification:
enter a number.
Output Specification:
print the sum of a given number of squares.
Sample Input:
5
Sample Output:
Sum of squares is: 55
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.util.Scanner; public class Main1 { public static void main(String [] args) { Scanner in=new Scanner(System.in); int i; int n=in.nextInt(); int sum=0; for(i=1;i<=n;i++) { sum+=i*i; } System.out.println("Sum of squares is: "+sum); } }