Java Program to Add the nth Square Series


Sum of nth squares series is a form of arithmetic progression where sum of squares is arranged in a sequence with initial term as 1, and n being the total number of terms.

12 + 22 + 32 + ….. + n2

In this article, we will discuss java program to add the nth square series.

Approach 1: Using Iterative approach

Let’s understand the logic for program with a simple example.

Example

If we take input as num = 6

It will be calculated as: 12 + 22 + 32 + 42 + 52 + 62 = 1 + 4 + 9 + 16 + 25 + 36 = 91

Therefore, output will be 91.

Using For loop

Syntax of For loop

for (initial expression; conditional expression; increment/decrement expression)
{
   // code to be executed
}

initial expression − executed once when loop begins.

conditional expression − code will be executed till conditional expression is true.

increment/decrement expression − to increment/decrement loop variable.

Example

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int addition = 0;
      int num = 6;  
      for(int i = 1; i <= num; i++) {
         addition += i * i; // same as addition = addition + i * i
      }
      System.out.println("Sum of nth series till " + num + "th terms = " + addition);
   }
}

Output

Sum of nth series till 6th terms = 91

In the above code, we have declared and initialized two variable named ‘num’ and ‘addition’. It is necessary to initialize the variables otherwise compiler will store a garbage value in it. The for loop is running from 1 to the value of ‘num’ and during each iteration sum of squares are getting stored in ‘addition’.

Using While loop

Syntax

while (conditional expression) 
{
   // code will be executed till conditional expression is true
   increment/decrement expression; 
   // to increment or decrement loop variable
}

Example

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int addition = 0;
      int num = 6;
      int i=0;
      while (i <= num) { 
         // loop is running till 6
         addition += i * i; 
         // to perform sum of squares 
         i++; // incrementing loop variable
      }
      System.out.println("Sum of nth series till " + num + "th terms = " + addition);
   }
}

Output

Sum of nth series till 6th terms = 91

Approach 2: Using Formula of Sum of Squares

All the counting numbers are called as natural numbers. Its range is from 1 to infinity. If we want to find sum of squares of n consecutive natural numbers then, we can use the given formula in our program −

12 + 22 + 32 + ... + num2 = ( num * ( num + 1 ) * ( 2 * num + 1 ) ) / 6

Let’s understand the formula with a example.

Example

If we take Input as num = 5

It will be calculated as: ( 5 * ( 5+1 ) * ( 2 * 5 + 1 )) / 6 = ( 5 * 6 * 11 ) / 6 = 330 / 6 = 55

Therefore, output will be 55.

Example

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int num = 5;
	   int addition = 0;
      addition = (num * (num + 1) * (2 * num + 1)) / 6; 
      // Using the above Formula
      System.out.println("Sum of nth series till " + num + "th terms = " + addition);
   } 
}

Output

Sum of nth series till 5th terms = 55

Conclusion

In this article, we have discussed two approaches to do java program to add the nth square series. First one by using iterative approach like for loop or while loop and second one by using the mathematical formula to find sum of squares of n consecutive natural numbers.

Updated on: 25-Apr-2023

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements