In the previous article we have discussed about Java Program to Print the Series 1 9 17 33 49 73 97 … N
In this article we are going to see how to print the series 2 4 7 12 21 38 71 …. N by using Java programming language.
Java Program to Print the Series 2 4 7 12 21 38 71 …. N
On observing the pattern carefully, we can see
First number starts from 2, then the next term follows a logic
Example:
2
2*2-0 = 4
4*2-1 = 7
7*2-2= 12
12*2-3 = 21 and so on.
2 | 4 | 7 | 12 | 21 | 38 | …… | N |
Let’s see different ways to print the series 2 4 7 12 21 38 71 …. N
Method-1: Java Program to Print the Series 2 4 7 12 21 38 71 …. N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n
. - Let declare an integer variable say ‘
result
’ and initialize it to 2 - Use a for loop from
i=0 to i<n-1
where the loop is incremented by 1 - Inside the for loop we will find the value of
result=(result*2)-i
- 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 number of terms "); int n = s.nextInt(); int result = 2; System.out.print(result); //for loop to print the series for (int i = 0; i < n-1; i++) { result=(result*2)-i; System.out.print(" "+result); } } }
Output: Enter the number of terms 5 2 4 7 12 21
Method-2: Java Program to Print the Series 2 4 7 12 21 38 71 …. N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n
. - Let declare integer variable say ‘
result
’ and initialize it to 2 - Declare and initialize an integer variable
i=0
- Continue a while loop
till i<n-1
, wherei
is incremented by 1. - Inside the for loop we will find the value of
result=(result*2)-i
- 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 number of terms "); int n = s.nextInt(); int result = 2; System.out.print(result); int i=0; while(i<n-1) { result=(result*2)-i; System.out.print(" "+result); i++; } } }
Output: Enter the number of terms 7 2 4 7 12 21 38 71
Method-3: Java Program to Print the Series 2 4 7 12 21 38 71 …. N By Using User Defined Method
Approach:
- The same logic as method 1 but this time we are moving the for inside a user-defined method.
- Create a Scanner class object.
- Prompt the user to enter a value for ‘
n
‘ as the number of terms in the series. - Call a user defined method
printSeries()
by passing n as parameter. - Inside the method, let declare an integer variable say ‘
result
’ and initialize it to 2 - Use a for loop
from i=0 to i<n-1
where the loop is incremented by 1 - Inside the for loop we will find the value of
result=(result*2)-i
- 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 number of terms "); 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) { int result = 2; System.out.print(result); //for loop to print the series for (int i = 0; i < n-1; i++) { result=(result*2)-i; System.out.print(" "+result); } } }
Output: Enter the number of terms 9 2 4 7 12 21 38 71 136 265
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: