Java Program to Print the Series 8 6 9 23 87 … N

In the previous article we have discussed about Java Program to print Series 7, 10, 8, 11, 9, 12, … N

In this article we are going to see how to print the series 8 6 9 23 87 … N by using Java programming language.

Java Program to Print the Series 8 6 9 23 87 … N

On observing the pattern carefully, we can see

1st number starts from 8 and it follows below logic for the next term.

Example:

8 x 1 – 2 = 6

6 x 2 – 3 = 9

9 x 3 – 4 = 23

23 x 4 – 5 = 87

87 x 5 – 6 = 429  and so on.

8 6 9 23 87 429 N

Let’s see different ways to print the series 8 6 9 23 87 … N

Method-1: Java Program to Print the Series 8 6 9 23 87 … 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 two integer variables say ‘result’, ‘sub’ and initialize it to 8, 2 respectively
  • 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)-sub and the sub is incremented by 1 in each iteration.
  • 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 = 8, sub = 2;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <= n-1; i++) 
        {
            result = (result * i) - sub; 
            System.out.print(" "+result);
            sub += 1;
        } 
    }
}
Output:

Enter the Nth term “N” 
5
8 6 9 23 87

Method-2: Java Program to Print the Series 8 6 9 23 87 … 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 two integer variables say ‘result’ and  ‘sub’ and initialize it to 8, 2 respectively
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n-1, where i is incremented by 1.
  • Inside the while loop we will find the value of result=(result*i)-sub and the sub is incremented by 1 in each iteration.
  • 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 = 8, sub = 2;
        System.out.print(result);
        int i=1;
        while(i<=n-1)
        {
            result = (result * i) - sub; 
            System.out.print("  "+result);
            sub +=1;
            i++;
        }      
    }
}
Output:


Enter the Nth term “N” 
7
8 6 9 23 87 429 2567

Method-3: Java Program to Print the Series 8 6 9 23 87 … 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 printSeries() by passing n as parameter.
  • Inside method let declare two integer variables say ‘result’, ‘sub’ and initialize it to 8, 2 respectively
  • 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)-sub and the sub is incremented by 1 in each iteration.
  • 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 = 8, sub = 2;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <=n-1; i++) 
        {
            result = (result * i) - sub; 
            System.out.print("  "+result);
            sub +=1;
        } 
    }
}

Output:


Enter the value of Nth term 'N' 
9
8 6 9 23 87 429 2567 17961 143679

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: