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

In this article we are going to see how to print the sum of series 3 + 7+ 13 + 21 + … + N by using Java programming language.

## Java Program to Print the Sum of Series 3 + 7+ 13 + 21 + … + N

On observing the pattern carefully, we can see

First number starts from 1, then the next term follows some logic

**Example:**

3

3+4=7

7+6=13

13+8=21 … so on

3 | 7 | 13 | 21 | 31 | 43 | ….. | N |

** ** +4 +6 +8 +10 +12

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

### Method-1: Java Program to Print the Sum of Series 3 + 7+ 13 + 21 + … + N 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 ‘
`result`

’ and initialize it to 3 - Let declare another integer variable say ‘
`add`

’ and initialize it to 4 - Use a for loop
`from i=1 to i<=n-1`

where the loop is incremented by 1 - Inside the for loop we will find the value of
`result=result+add`

and`add=add+2`

- 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 result = 3; int add = 4; //for loop to print the series for (int i = 1; i <= n-1; i++) { result +=add; add+=2; } System.out.print("The sum of the series is "+result); } }

Output: Enter the Nth term “N” 5 The sum of the series is 31

### Method-2: Java Program to Print the Sum of Series 3 + 7+ 13 + 21 + … + N 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 ‘
`result`

’, ‘`add`

’ and initialize it to 3, 4 respectively. - Declare and initialize an integer variable
`i=1`

- Continue a while loop till
`i<=n-1`

, where`i`

is incremented by 1 - Inside while loop find the value of
`result = result+add`

and`add=add+2`

- 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 result=3; int add = 4; int i=1; while(i<=n-1) { result +=add; add+=2; i++; } System.out.print("The sum of the series is "+result); } }

Output: Enter the Nth term “N” 7 The sum of the series is 57

### Method-3: Java Program to Print the Sum of Series 3 + 7+ 13 + 21 + … + N By Using User Defined Method

### Approach:

- Create a Scanner class object.
- Prompt the user to enter the which holds the number of terms in the series.
- Call a user defined method
`printSeries()`

by passing`n`

as parameter. - Inside the method use a for loop to find the series.
- Let declare integer variables say ‘
`result`

’, ‘`add`

’ and initialize it to 3,4 respectively. - Use a for loop from
`i=1 to i<=n-1`

incremented by 1 - Inside the for loop we will find the value of
`result+=add and add+=2`

- 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 result = 3, add = 4; System.out.print(result); //for loop to print the series for (int i = 1; i <=n-1; i++) { result +=add; add+=2; } System.out.print("The sum of the series is "+result); } }

Output: Enter the value of Nth term 'N' 9 3The sum of the series is 91

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

**Related Java Programs:**