In the previous article we have discussed about Java Program to Print the Series 8 14 24 40 … N
In this article we are going to see how to print the series 1 2 6 21 88 … N by using Java programming language.
Java Program to Print the Series 1 2 6 21 88 … N
On observing the pattern carefully, we can see 1st number starts from 1
Then the next number follows a logic
1 1 x 1 + 1 = 2 2 x 2 + 2 = 6 6 x 3 + 3 = 21 21 x 4 + 4 = 88 … and so on
Example:
1 | 2 | 6 | 21 | 88 | 445 | …… | N |
Let’s see different ways to print the series 1 2 6 21 88 … N
Method-1: Java Program to Print the Series 1 2 6 21 88 … 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 an integer variable say ‘
result
’ and initialize it to 1 - 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
result=result*i+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 number of terms in series "); int n = s.nextInt(); int result = 1; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { result = result*i+i; System.out.print(" "+result); } } }
Output: Enter number of terms in series 5 1 2 6 21 88
Method-2: Java Program to Print the Series 1 2 6 21 88 … 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 an integer variable say ‘
result
’ and initialize it to 1 - Declare and initialize an integer variable
i=1
- 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*i+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 number of terms in series "); int n = s.nextInt(); int result=1; System.out.print(result); int i=1; while(i<=n-1) { result = result*i+i; System.out.print(" "+result); i++; } } }
Output: Enter number of terms in series 7 1 2 6 21 88 445 2676
Method-3: Java Program to Print the Series 1 2 6 21 88 … 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 an integer variable say ‘
result
’ and initialize it to 1 - 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
result=result*i+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 number of terms in series "); 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 = 1; System.out.print(result); //for loop to print the series for (int i = 1; i <=n-1; i++) { result = result*i+i; System.out.print(" "+result); } } }
Output: Enter number of terms in series 9 1 2 6 21 88 445 2676 18739 149920
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.
Related Java Programs: