Java Program to Print the Series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2

In the previous article we have discussed about Java Program to Print the Sum of Series 1+ 2 + 3 + 4 + … + N

In this article we are going to see how to print the sum of series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2 by using Java programming language.

Java Program to Print the Series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2

On observing the pattern carefully, we can see it is in the form of a^2. Where ‘a‘ starts from 1, then the next term is incremented by 1 with the previous term and finally all the terms are added.

Example:

Suppose value of n = 5

1^2+2^2+3^2+4^2+5^2 = 1+4+9+16+25 = 55

1^2 2^2 3^2 4^2 5^2 6^2 …… N^2

Let’s see different ways to sum of series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2

Method-1: Java Program to Print the Series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2 By Using For Loop

Approach: 

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an integer variable say ‘sum’ and initialize it to 0.
  • Use a for loop from i=1 to i<=n where the loop is incremented by 1.
  • Inside the for loop we will find the value of sum=sum+(i*i)
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        int sum = 0;
        //for loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            sum+=(i*i); 
        } 
        System.out.print("The sum of the series is "+sum);
    }
}
Output:

Enter the Nth term “N” 
5
The sum of the series is 55

Method-2: Java Program to Print the Series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2 By Using While Loop

Approach: 

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare integer variable say ‘sum’ and initialize it to 0
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n, where i is incremented by 1.
  • Inside while loop find the value of sum+=i*i
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        int sum = 0;
        int i=1;
        while(i<=n)
        {
            sum +=i*i; 
            i++;
        } 
  	    System.out.print("The sum of the series is  "+sum);
    }
}
Output:

Enter the Nth term “N” 
7
The sum of the series is 140

Method-3: Java Program to Print the Series 1^2 + 2^2 + 3^2 + 4^2 + …… + N^2 By Using User Defined Method

Approach: 

  • Create a Scanner class object.
  • Prompt the user to enter a value of n which holds the number of terms in the series.
  • Call a user defined method printSeries() by passing n as parameter.
  • Inside the method declare integer variable say ‘sum’ and initialize it to 0 and we will use a for loop to find the sum series.
  • Use a for loop from i=1 to i<=n where the loop is incremented by 1
  • Inside the for loop we will find the value of sum=sum+(i*i)
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of Nth term 'N' ");
        int n = s.nextInt();
        // calling printSeries method to print the series
        printSeries(n);
    }
    
    //printSeries metthod to print the series
    public static void printSeries(int n)
    {
        int sum=0;
        //for loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            sum+=(i*i); 
        } 
        System.out.print("The sum of the series is "+sum);
    }
}
Output:

Enter the value of Nth term 'N' 
9
The sum of the series is 285

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: