Print powers of 2 java: In the previous article we have discussed about Java Program to Print Series 0 3 8 15 24 35 48…N
In this article we are going to see how to print the series 2 4 16 256 … N by using Java programming language.
Java Program to Print the Series 2 4 16 256 … N
On observing the pattern carefully, we can see
1st number starts from 2
Then the next number is the square of the previous number
Example:
2 | 4 | 16 | 256 | 65536 | …… | N-1 | N |
2^2 4^2 16^2 256^2 65536^2 (N-1)^2
Let’s see different ways to print the series 2 4 16 256 … N
Method-1: Java Program to Print the Series 2 4 16 256 … 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 a long variable say ‘
result
’ 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 square of the result and then again we will store that value with the
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(); long result = 2; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { result *=result; System.out.print(" "+result); } } }
Output: Enter the Nth term “N” 5 2 4 16 256 65536
Method-2: Java Program to Print the Series 2 4 16 256 … 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 a long variable say ‘
result
’ 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 square of the result and then again we will store that value with the
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(); long result=2; System.out.print(result); int i=1; while(i<=n-1) { result *=result; System.out.print(" "+result); i++; } } }
Output: Enter the Nth term “N” 6 2 4 16 256 65536 4294967296
Method-3: Java Program to Print the Series 2 4 16 256 … 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 passingn
as parameter. - Inside method declare a long variable say ‘
result
’ 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 square of the result and then again we will store that value with the
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); } //printSeries metthod to print the series public static void printSeries(int n) { long result = 2; System.out.print(result); //for loop to print the series for (int i = 1; i <=n-1; i++) { result *=result; System.out.print(" "+result); } } }
Output: Enter the value of Nth term 'N' 6 2 4 16 256 65536 4294967296
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Programs: