# Java Program to Print the Series 2 3 10 39 117 885 … N

In the previous article we have discussed about Java Program to Print the Series 0 4 18 48 100 180 … N

In this article we are going to see how to print the series 2 3 10 39 117 885 … N by using Java programming language.

## Java Program to Print the Series 2 3 10 39 117 885 … N

On observing the pattern carefully, we can see 1st number starts from 2

Then the next number follows a logic

2
2*1+(1*1)=3
3*2+(2*2)=10
10*3+(3*3)=39
39*4+(4*4)=172
172*5+(5*5)=885 and so on.

For example:

 2 3 10 39 117 885 …… N

*1+(1^2)     *2+(2^2)     *3+(3^2)     *4+(4^2)       *5+(5^2)

Java Program to print the series 2 3 10 39 117 885 … N.

### Method-1: Java Program to Print the Series 2 3 10 39 117 885 … 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 2
• 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*i)+(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 number of elements” ");
int n = s.nextInt();
int result = 2;
System.out.print(result);
//for loop to print the series
for (int i = 1; i <= n-1; i++)
{
result = (result*i) + (i*i);
System.out.print(" "+result);
}
}
}
Output:

Enter number of elements
5
2 3 10 39 172

### Method-2: Java Program to Print the Series 2 3 10 39 117 885 … 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 an integer variable say ‘result’ and initialize it to 2
• 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*i + (i^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=2;
System.out.print(result);
int i=1;
while(i<=n-1)
{
result = (result*i) + (i*i);
System.out.print(" "+result);
i++;
}
}
}
Output:

Enter number of elements
7
2 3 10 39 172 885 5346

### Method-3: Java Program to Print the Series 2 3 10 39 117 885 … N By Using User Defined Method

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 2
• Call a user defined method printSeries() by passing n as parameter.
• Inside the method 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*i)+(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() method to print the series
public static void printSeries(int n)
{
int result = 2;
System.out.print(result);
//for loop to print the series
for (int i = 1; i <=n-1; i++)
{
result = (result*i) + (i*i);
System.out.print(" "+result);
}
}
}

Output:

Enter number of elements
9
2 3 10 39 172 885 5346 37471 299832

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: