# Java Program to Print the Series 7 26 63 124 215 … N

In the previous article we have discussed about Java Program to Print the Series 2 3 10 39 117 885 … N

In this article we are going to see how to print the series 7 26 63 124 215 .. N by using Java programming language.

## Java Program to Print the Series 7 26 63 124 215 … N

On observing the pattern carefully, we can see the series follows a logic

2^3-1 = 7
3^3-1 = 26
4^3-1 = 63
5^3-1 = 124 … and so on.

Example:

 7 26 63 124 215 342 …… N

2^3-1          3^3+1          4^3-1         5^3-1          6^3-1           7^3-1

Let’s see different ways to print the series 7 26 63 124 215 .. N.

### Method-1: Java Program to Print the Series 7 26 63 124 215 … 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 a double variable say ‘result
• Use a for loop from i=2 to i<=n+1 where the loop is incremented by 1
• Inside the for loop we will find the value of result=i^3 - 1
• 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 = 2; i <= n+1; i++)
{
result = Math.pow(i,3)-1;
System.out.print((int)result+" ");
}
}
}

Output:

Enter the Nth term “N”
5
7 26 63 124 215

### Method-2: Java Program to Print the Series 7 26 63 124 215 … 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 a double variable say ‘result
• Declare and initialize an integer variable i=2
• Continue a while loop till i<=n+1, where i is incremented by 1.
• Inside while loop find the value of result = (i^3)-1
• 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=2;
while(i<=n+1)
{
result = Math.pow(i,3)-1;
System.out.print((int)result+" ");
i++;
}
}
}

Output:

Enter the Nth term “N”
7
7 26 63 124 215 342 511

### Method-3: Java Program to Print the Series 7 26 63 124 215 … 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.
• Call a user defined method printSeries() by passing n as parameter.
• Inside method declare a double variable say ‘result
• Use a for loop from i=2 to i<=n+1 where the loop is incremented by 1
• Inside the for loop we will find the value of result=i^3 - 1
• 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 = 2; i <=n+1; i++)
{
result = Math.pow(i,3)-1;
System.out.print((int)result+" ");
}
}
}

Output:

Enter the value of Nth term 'N'
9
7 26 63 124 215 342 511 728 999

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: