Java Program to Print the Series 8 14 24 40 … N

In the previous article we have discussed about Java Program to Print the Series 7 26 63 124 215 … N

In this article we are going to see how to print the series 8 14 24 40 … N by using Java programming language.

Java Program to Print the Series 8 14 24 40 … N

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

Then the next number follows a logic

8
8 x 1.5 + 2 = 14
14 x 1.5 + 3 = 24
24 x 1.5 + 4 = 40
40 x 1.5 + 5 = 65 … and so on

Example:

8 14 24 40 65 81 …… N

                        *1.5+2             *1.5+3            *1.5+4              *1.5+5             *1.5+6

Let’s see different ways to print the series 8 14 24 40 … N

Method-1: Java Program to Print the Series 8 14 24 40 … 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 an double variable say ‘result’ and initialize it to 8
  • Let declare another integer variable say ‘add’ 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*1.5)+add and add=add+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 numbers of terms in series ");
        int n = s.nextInt();
        double result = 8;
        int add = 2;
        System.out.print((int)result);
        //for loop to print the series
        for (int i = 1; i <= n-1; i++) 
        {
            result =(result*1.5)+add; 
            System.out.print(" "+(int)result);
            add+=1;
        } 
    }
}
Output: 

Enter numbers of terms in series
5
8 14 24 40 65

Method-2: Java Program to Print the Series 8 14 24 40 … 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 an double variable say ‘result’ and initialize it to 8
  • Let declare another integer variable say ‘add’ 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 the while loop we will find the value of result=(result*1.5)+add and add=add+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 numbers of terms in series ");
        int n = s.nextInt();
        double result=8;
        int add = 2;
        System.out.print((int)result);
        int i=1;
        while(i<=n-1)
        {
            result =(result*1.5)+add; 
            System.out.print(" "+(int)result);
            add+=1;
            i++;
        }      
    }
}
Output:

Enter numbers of terms in series
7
8 14 24 40 65 103 162

Method-3: Java Program to Print the Series 8 14 24 40 … 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 declare an double variable say ‘result’ and initialize it to 8
  • Declare another integer variable say ‘add’ 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*1.5)+add and add=add+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 numbers of terms in series ");
        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)
    {
        double result = 8, add = 2;
        System.out.print((int)result);
        //for loop to print the series
        for (int i = 1; i <=n-1; i++) 
        {
            result =(result*1.5)+add; 
            System.out.print(" "+(int)result);
            add+=1;
        } 
    }
}
Output:

Enter numbers of terms in series
9
8 14 24 40 65 103 162 251 386

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: