Java Program to Find the Sum of Series (1*2) + (2*3) + (3*4) + …… + N

In the previous article, we have discussed about Java Program to Print Series 9 18 27 36 45 54 …N

In this article we are going to see how to find the sum of the series (1*2) + (2*3) + (3*4) + …… + N by using java programming language.

Java Program to Find the Sum of Series (1*2) + (2*3) + (3*4) + …… + N

On observing the pattern carefully, we can see each term is like (a*b) format. Value of ‘a‘ starts from 1 and ‘b‘ value is like ‘a+1‘ and in next term of series previous ‘b‘ value is the value of ‘a

Example:

N = 5

1*2 + 2*3 + 3*4 + 4*5 + 5*6 = 2+6+12+20+30 = 70

Let’s see different ways to find the sum of the series (1*2) + (2*3) + (3*4) + …… + N

Method-1: Java Program to Find the Sum of Series (1*2) + (2*3) + (3*4) + …… + N By Using For Loop

Approach:

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

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 last number 'n' ");
        int n = s.nextInt();
        //for loop to print the series
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum += i * (i + 1);
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of last number 'n' 
5
Sum of the series is 70

Method-2: Java Program to Find the Sum of Series (1*2) + (2*3) + (3*4) + …… + N By Using While Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the value of number of terms in series.
  • Declare an integer variable say ‘sum‘ and initialize it to 0.
  • Prompt the user to enter a number as value of n.
  • Use a while loop from i=1 till i<=n
  • Inside while loop we will multiply i*(i+1) and add with sum.
  • Print the result.

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 last number 'n' ");
        int n = s.nextInt();
        // while loop to print the series
        int sum = 0;
        int i = 1;
        while(i <= n)
        {
            sum += i * (i + 1);
             i++;
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of last number 'n' 
9
Sum of the series is 330

Method-3: Java Program to Find the Sum of Series (1*2) + (2*3) + (3*4) + …… + N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the value of number of terms in series.
  • Prompt the user to enter a number as value of n.
  • Call a user defined method m1() and pass ‘n‘ as parameter.
  • Inside method declare an integer variable say ‘sum‘ and initialize it to 0.
  • Use a for loop from i=1 to i<=n
  • Inside for loop we will multiply i*(i+1) and add with sum.
  • Print the result.

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 last exponent 'n' ");
        int n = s.nextInt();
       // calling m1 method to print the series
        m1(n);
   }
    public static void m1(int n)
    {
        //for loop to print the series
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum += i * (i + 1);
        System.out.println("Sum of the series is  " + sum);
    }
}
Output:

Enter the value of last exponent 'n' 
10
Sum of the series is 440

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: