Java program to print sum square of n Natural number
Java code print sum of square of n Natural number
The sum of squares of the first n natural numbers is found by adding up all the squares.
Using Loops − the code loops through the digits until n and find their square, then add this to a sum variable that outputs the sum.
![]() |
sum of n natural number |
import java.util.Scanner;
public class SumOfSquares
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Integer: ");
int n = sc.nextInt();
System.out.println("User entered digit: " + n);
int sum = 0;
for (int x=1;x<=n;x++) {
sum = sum + (x*x);
}
System.out.println("sum of digit is " +sum);
}
}
Input - 5
Output - 55
Explanation - 12 + 22 + 32 + 42 + 52
Comments
Post a Comment
Please do not write any spam link in the comment box.