What should be the next number in the following series? 3 9 27 81 243 – Java Program to Print the Series 3 9 27 81 243 729 …N

What should be the next number in the following series? 3 9 27 81 243: In the previous article we have discussed about Java Program to Print Series 7 14 28 56 112 224…N

In this article we are going to see how to print the series 3 9 27 81 243 729 …N by using Java programming language.

Java Program to Print the Series 3 9 27 81 243 729 …N

On observing the pattern carefully, we can see the series starts from 3 and the next element is the multiplication of previous element with 3.

Example:

When value of N=5
Then series=
3 9 27 81 243

Let’s see different ways to print the series 3 9 27 81 243 729 …N

Method-1: Java Program to Print the Series 3 9 27 81 243 729 …N By Using For Loop

Approach:

  • Declare an integer variable say ‘n’ which holds the value of 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‘ & initialize it to 3
  • Use a for loop from i =1 to i<n incremented by 1
  • Inside for loop we will increment the result with 3 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 Nth term “N” ");
        int n = s.nextInt();
        int result = 3;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i < n; i++) 
        {
            result *=3; 
            System.out.print(" "+result);
        } 
    }
}
Output:

Enter the Nth term “N” 
5
4 16 64 256 1024

Method-2: Java Program to Print the Series 3 9 27 81 243 729 …N By Using While Loop

Approach:

  • Declare an integer variable say ‘n’ which holds the value of 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‘ & initialize it to 3
  • 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 3 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 Nth term “N” ");
        int n = s.nextInt();
       int result=3;
       System.out.print(result);
       int i=1;
       while(i<n)
       {
           result *=3; 
           System.out.print(" "+result);
           i++;
       }      
    }
}
Output:

Enter the Nth term “N” 
7
4 16 64 256 1024 4096 16384

Method-3: Java Program to Print the Series 3 9 27 81 243 729 …N By Using While Loop

Approach: 

  • Declare an integer variable say ‘n’ which holds the value of 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 3.
  • Use a for loop from i =1 to i<n incremented by 1.
  • Inside for loop we will increment the result with 3 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 Nth term 'N' ");
        int n = s.nextInt();
       // calling printSeries() method to print the series
        printSeries(n);
   }
    public static void printSeries(int n)
    {
        int result = 3;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i < n; i++) 
        {
            result *=3; 
            System.out.print(" "+result);
       } 
    }
}
Output:

Enter the value of Nth term 'N' 
7
4 16 64 256 1024 4096 16384

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Practice yourself:

  1. Write A Program To Generate The First N Terms In Series 3, 9, 27, 81,…
  2. What Should Be The Next Number In The Following Series? 3 9 27 81 243
  3. What Would Be The Next Number In The Following Series
  4. Simplify 1-3 9-27 81-243 729
  5. 3, 9, 27, 81, 243, 729,…
  6. 3….9….27….81
  7. 3, 9, 27, 81, 243, 729,

Related Java Programs: