In the previous article we have discussed about Java Program to print Series 2 12 36 80 150 … N
In this article we are going to see how to print the series Print Series 6 14 36 98 … N by using Java programming language.
Java Program to Print the Series 6 14 36 98 … N
On observing the pattern carefully, we can see Math.pow(1,i)+Math.pow(2,i)+Math.pow(3,i)
where i
starts from 1 and in each term incremented by 1 and goes up to n
.
Example:
6 = 1^1 + 2^1 + 3^1
14 = 1^2 + 2^2 + 3^2
36 = 1^3 + 2^3 + 3^3
98 = 1^4 + 2^4 + 3^4 ……
N = 1^n + 2^n + 3^n
6 | 14 | 36 | 98 | 276 | … | N |
Method-1: Java Program to Print the Series 6 14 36 98 … N By Using 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 double variable ‘
result
‘. - Use a for loop from
i=1 to i<=n
, where the loop is incremented by 1. - Inside the loop we will find the value of
1^i + 2^i + 3^i
of the Nth term and then store the 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(); double result; //for loop to print the series for (int i = 1; i <= n; i++) { result =Math.pow(1,i)+Math.pow(2,i)+Math.pow(3,i); System.out.print((int)result+" "); } } }
Output: Enter the Nth term “N” 6 6 14 36 98 276 794
Method-2: Java Program to Print the Series 6 14 36 98 … 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 double variable ‘
result
‘. - Declare and initialize an integer variable
i=1
- Use a while loop till
i<=n
, where the loop is incremented by 1. - Inside the loop we will find the value of
1^i + 2^i + 3^i
of the Nth term and then store the 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(); double result; int i=1; while(i<=n) { result =Math.pow(1,i)+Math.pow(2,i)+Math.pow(3,i); System.out.print((int)result+" "); i++; } } }
Output: Enter the Nth term “N” 7 6 14 36 98 276 794 2316
Method-3: Java Program to Print the Series 6 14 36 98 … 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 say
printSeries()
by passingn
as parameter. - Inside method declare a double variable ‘
result
‘. - Use a for loop from
i=1 to i<=n
, where the loop is incremented by 1. - Inside the loop we will find the value of
1^i + 2^i + 3^i
of the Nth term and then store the 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) { double result; //for loop to print the series for (int i = 1; i <=n; i++) { result =Math.pow(1,i)+Math.pow(2,i)+Math.pow(3,i); System.out.print((int)result+" "); } } }
Output: Enter the value of Nth term 'N' 5 6 14 36 98 276
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Programs: