Java Program to Print Series 1 2 4 8 16 32 64 128 …N

In the previous article we have discussed about Java Program to Print Series 8 16 24 32 40 …N

In this article we are going to see how to print the series 1 2 4 8 16 32 64 128 …N by using Java programming language.

Java Program to Print Series 1 2 4 8 16 32 64 128 …N

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

Example:

Suppose value of N=5
Then series=
1 2 4 8 16

Let’s see different ways to print the series 1 2 4 8 16 32 64 128 …N

Method-1: Java Program to Print Series 1 2 4 8 16 32 64 128 …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 1
  • 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 number of terms ");
        int n = s.nextInt();
        int result = 1;
        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 number of terms
5
1 2 4 8 16

Method-2: Java Program to Print Series 1 2 4 8 16 32 64 128 …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 1
  • 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 number of terms ");
       int n = s.nextInt();
       int result=1;
       System.out.print(result);
       int i=1;
       while(i<n)
       {
           result *=2; 
           System.out.print(" "+result);
           i++;
       }      
    }
}
Output:

Enter the value of number of terms
5
1 2 4 8 16

Method-3: Java Program to Print Series 1 2 4 8 16 32 64 128 …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 1
  • 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 number of terms ");
        int n = s.nextInt();
       // calling printSeries method to print the series
        printSeries(n);
   }
    public static void printSeries(int n)
    {
        int result = 1;
        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 number of terms
5
1 2 4 8 16

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: