Using a while loop, write a program to calculate and print the sum of a given number of squares.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
int sum=0;
int i=1;
System.out.println("Enter n: ");
n=in.nextInt();
while(i<=n)
{
sum=sum+i*i;
i++;
}
System.out.println("Sum of squares of numbers from 1 to n is : "+sum);
}
}
Comments
Leave a comment