# Java Program to Print the Series 2 12 36 80 150 … N

In the previous article we have discussed about Java Program to print Series 1 2 10 37 101 226 … N

In this article we are going to see how to print the series 2 12 36 80 150 … N by using Java programming language.

## Java Program to Print the Series 2 12 36 80 150 … N

On observing the pattern carefully, you can see the series is in the form of i^3+i^2, where i value starts from 1 and in next term incremented by 1 and it goes on increasing up to N.

Example:

 2 12 36 80 150 252 … N

1^3+1^2   2^3+2^2    3^3+3^2    4^3+4^2     5^3+5^2     6^3+6^2                         n^3 + n^2

Let’s see different ways to print the series 2 12 36 80 150 … N.

### Method-1: Java Program to Print the Series 2 12 36 80 150 … N By Using For Loop

Approach:

• Create Scanner class object.
• Declare an integer variable say ‘n’ which holds the Nth term of the series.
• Prompt the user to enter a number as value of ‘n’.
• Let declare a double variable say ‘result
• Use a for loop from i=1 to i<=n, where the loop is incremented by 1.
• Inside the loop we will find the value of i^3 + i^2 and then store the value with the result.
• 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();
double result;
//for loop to print the series
for (int i = 1; i <= n; i++)
{
result =Math.pow(i,3)+Math.pow(i,2);
System.out.print((int)result+" ");
}
}
}

Output:

Enter the Nth term “N”
5
2 12 36 80 150

### Method-2: Java Program to Print the Series 2 12 36 80 150 … N By Using While Loop

Approach:

• Create Scanner class object.
• Declare an integer variable say ‘n’ which holds the Nth term of the series.
• Prompt the user to enter a number as value of ‘n’.
• Let declare a double variable say ‘result
• Declare and initialize an integer variable i=1
• Continue a while loop till i<=n, where the loop is incremented by 1.
• Inside the loop we will find the value of i^3 + i^2 and then store the value with the result.
• 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();
double result;
int i=1;
while(i<=n)
{
result =Math.pow(i,3)+Math.pow(i,2);
System.out.print((int)result+" ");
i++;
}
}
}

Output:

Enter the Nth term “N”
7
2 12 36 80 150 252 392

### Method-3: Java Program to Print the Series 2 12 36 80 150 … N By Using User Defined Method

Approach:

• Create Scanner class object.
• Declare an integer variable say ‘n’ which holds the Nth term of the series.
• Prompt the user to enter a number as value of ‘n’.
• Call a user defined method say printSeries() by passing n as parameter.
• Inside method declare a double variable say ‘result
• Declare and initialize an integer variable i=1
• Continue a while loop till i<=n, where the loop is incremented by 1.
• Inside the loop we will find the value of i^3 + i^2 and then store the value with the result.
• 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)
{
double result;
//for loop to print the series
for (int i = 1; i <= n; i++)
{
result =Math.pow(i,3)+Math.pow(i,2);
System.out.print((int)result+" ");
}
}
}

Output:

Enter the value of Nth term 'N'
6
2 12 36 80 150 252

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: