2 … 3 … 6 … 11 … 18 … ?: In the previous article we have discussed about Java Program to print Series 2 4 16 256 … N
In this article we are going to see how to print the series 3 6 11 18 27 … N by using Java programming language. We provided well information to grasp the knowledge easily without any difficulty to maintain the subject on your fingertips. We mainly focus on beginners on this concept 3,6,11,18,27, 3,6,11,18,?
Java Program to Print the Series 3 6 11 18 27 … N
On observing the pattern carefully, we can see
1st number starts from 3, then the next number follows below logic
3
3+3 = 6
6+5 = 11
11+7 = 18
18+9 = 27 and so on.
Example:
3 | 6 | 11 | 18 | 27 | …… | N-1 | N |
+3 +5 +7 +9
Let’s see different ways to print the series 3 6 11 18 27 … N
Method-1: Java Program to Print the Series 3 6 11 18 27 … 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 3 and print it as first term value. - Let declare another integer variable say ‘
add
’ and initialize it to 3 - 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+add
andadd=add+2
. - 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(); int result = 3; int add = 3; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { result +=add; System.out.print(" "+result); add+=2; } } }
Output: Enter the Nth term “N” 5 3 6 11 18 27
Method-2: Java Program to Print the Series 3 6 11 18 27 … 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 3 and print it as first term value. - Let declare another integer variable say ‘
add
’ and initialize it to 3 - Declare and initialize an integer variable
i=1
- Continue a while loop
till i<=n-1
, wherei
is incremented by 1. - Inside the while loop we will find the value of
result=result+add
andadd=add+2
. - 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(); int result=3; int add = 3; System.out.print(result); int i=1; while(i<=n-1) { result +=add; System.out.print(" "+result); add+=2; i++; } } }
Output: Enter the Nth term “N” 6 3 6 11 18 27 38
Method-3: Java Program to Print the Series 3 6 11 18 27 … 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 3 and print it as first term value. - Declare another integer variable say ‘
add
’ and initialize it to 3 - 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+add
andadd=add+2
. - 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) { int result = 3, add = 3; System.out.print(result); //for loop to print the series for (int i = 1; i <=n-1; i++) { result +=add; System.out.print(" "+result); add+=2; } } }
Output: Enter the value of Nth term 'N' 7 3 6 11 18 27 38 51
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Also Refer: Java Program to Print the Series 0 7 26 63 …N
Check out once:
- Write A Program To Print A Number Series [3, 6, 11, 18, 27, 38, 51, …] Upto N Times”.
- 1 3 6 11 18.
Related Java Programs: