In the previous article we have discussed about Java Program to Print Series 3 9 27 81 243 729 …N
In this article we are going to see how to print the series 4 16 64 256 1024 6144…N using Java programming language
Java Program to Print Series 4 16 64 256 1024 6144…N
On observing the pattern carefully, we can see the series starts from 4 and the next element is the multiplication of previous element with 4.
Example:
When value of N=5 Then series= 4 16 64 256 1024
Let’s see different ways to print the series 4 16 64 256 1024 6144…N
Method-1: Java Program to Print Series 4 16 64 256 1024 6144…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 4 - Use a for loop
from i =1 to i<nincremented by 1 - Inside for loop we will increment the result with 4 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 = 4;
System.out.print(result);
//for loop to print the series
for (int i = 1; i < n; i++)
{
result *=4;
System.out.print(" "+result);
}
}
}
Output: Enter the value of number of terms 5 4 16 64 256 1024
Method-2: Java Program to Print Series 4 16 64 256 1024 6144…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 4 - Declare and initialize an integer variable
i=1 - Continue a while loop till
i<nincremented by 1. - Inside while loop we will increment the result with 4 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=4;
System.out.print(result);
int i=1;
while(i<n)
{
result *=4;
System.out.print(" "+result);
i++;
}
}
}
Output: Enter the value of number of terms 7 4 16 64 256 1024 4096 16384
Method-3: Java Program to Print Series 4 16 64 256 1024 6144…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 4. - Use a for loop
from i =1 to i<nincremented by 1. - Inside for loop we will increment the result with 4 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 = 4;
System.out.print(result);
//for loop to print the series
for (int i = 1; i < n; i++)
{
result *=4;
System.out.print(" "+result);
}
}
}
Output: Enter the value of number of terms 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.
Related Java Programs: