Java Program to Print the Series 6 12 24 48 96 …N

In the previous article we have discussed about Java Program to Print Series 5 10 20 40 80 160 …N

In this article we are going to see how to print the series  6 12 24 48 96 …N by using Java programming language

Java Program to Print the Series 6 12 24 48 96 …N

On observing the pattern carefully, we can see the series starts from 6 and next element is 2 times of previous element.

Example:

Suppose value of N=5
Then series=
6 12 24 48 96

Let’s see different ways to print the series 6 12 24 48 96 …N

Method-1: Java Program to Print the Series 6 12 24 48 96 …N By Using For Loop

Approach:

  • Declare an integer variable say ‘n’ which holds number of terms in the series.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Let declare an integer variable ‘result‘ and initialize it to 6
  • Use a for loop from i=1 to i<n incremented by 1
  • Inside for loop we will increment the result with 2 times of the previous 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 “N” ");
        int n = s.nextInt();
        int result = 6;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i < n; i++) 
        {
            result *=2; 
            System.out.print(" "+result);
       } 
    }
}
Output:

Enter the value of  “N” 
5
3 6 12 24 48

Method-2: Java Program to Print the Series 6 12 24 48 96 …N By Using While Loop

Approach:

  • Declare an integer variable say ‘n’ which holds number of terms in the series.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Let declare an integer variable ‘result‘ and initialize it to 6
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<n incremented by 1
  • Inside while loop we will increment the result with 2 times of the previous result.
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of “N” ");
       int n = s.nextInt();
       int result=6;
       System.out.print(result);
       int i=1;
       while(i<n)
       {
           result *=2; 
           System.out.print(" "+result);
           i++;
       }      
    }
}
Output:

Enter the value of “N” 
7
3 6 12 24 48 96 192

Method-3: Java Program to Print the Series 6 12 24 48 96 …N By Using User Defined Method

Approach:

  • Declare an integer variable say ‘n’ which holds number of terms in the series.
  • Create Scanner class object.
  • 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 an integer variable ‘result‘ and initialize it to 6
  • Use a for loop from i=1 to i<n incremented by 1
  • Inside for loop we will increment the result with 2 times of the previous 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 'N' ");
        int n = s.nextInt();
       // calling printSeries method to print the series
        printSeries(n);
   }
    public static void printSeries(int n)
    {
        int result = 6;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <n; i++) 
        {
            result *=2; 
            System.out.print(" "+result);
       } 
    }
}
Output:

Enter the value of N' 
9
3 6 12 24 48 96 192 384 768

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: