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
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

Popular posts from this blog

Java Program for shop Bill to Calculate total amount of product with discount

RCM|Consequences of imbalanced Nutrition|WHO share daily need

Learn Data structures and algorithms concepts free online with certificate