Java Program to Print the Series 7 14 28 56 112 224…N

In the previous article we have discussed about Java Program to Print Series 6 12 24 48 96 192…N

In this article we are going to see how to print the series 7 14 28 56 112 224…N using Java programming language

Java Program to Print the Series 7 14 28 56 112 224…N

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

Example:

Suppose value of N=5
Then the series =
7 14 28 56 112

Let’s see different ways to print the series 7 14 28 56 112 224…N

Method-1: Java Program to Print the Series 7 14 28 56 112 224…N By Using For Loop

Approach: 

  • Declare an integer variable say ‘n’ which holds the number of elements 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 7.
  • 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 = 7;
        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
7 14 28 56 112

Method-2: Java Program to Print the Series 7 14 28 56 112 224…N By Using While Loop

Approach: 

  • Declare an integer variable say ‘n’ which holds the number of elements 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 7.
  • Declare and initialize an integer variable i=1
  • Continue a while loop till 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)
    {
       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=7;
       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
7
7 14 28 56 112 224 448

Method-3: Java Program to Print the Series 7 14 28 56 112 224…N By Using User Defined Method

Approach: 

  • Declare an integer variable say ‘n’ which holds the number of elements 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.
  • Let declare an integer variable ‘result‘ and initialize it to 7.
  • 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 = 7;
        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
9
7 14 28 56 112 224 448 896 1792

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: