In the previous article we have discussed about Java Program to Print the Series 2 4 7 12 21 38 71 … N
In this article we are going to see how to print the series 0 2 6 12 20 30 42 … N by using Java programming language.
Java Program to Print the Series 0 2 6 12 20 30 42 … N
On observing the pattern carefully, we can see
First number starts from 0, then the next term follows below logic
Example:
0
0+2=2
2+4=6
6+6=12
12+8=20 … so on.
0 | 2 | 6 | 12 | 20 | 30 | …… | N |
+2 +4 +6 +8 +10
Let’s see different ways to print the series 0 2 6 12 20 30 42 … N
Method-1: Java Program to Print the Series 0 2 6 12 20 30 42 … N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of elements 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 0 - Let declare another integer variable say ‘
add
’ and initialize it to 2 - 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 number of elements "); int n = s.nextInt(); int result = 0; int add=2; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { result +=add; add+=2; System.out.print(" "+result); } } }
Output: Enter the number of elements 5 0 2 6 12 20
Method-2: Java Program to Print the Series 0 2 6 12 20 30 42 … N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of elements 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 0 - Let declare another integer variable say ‘
add
’ and initialize it to 2 - Declare and initialize an integer variable
i=1
- Continue a while loop
till i<=n-1
, where i 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=0; int add = 2; System.out.print(result); int i=1; while(i<=n-1) { result +=add; add+=2; System.out.print(" "+result); i++; } } }
Output: Enter the number of elements 7 0 2 6 12 20 30 42
Method-3: Java Program to Print the Series 0 2 6 12 20 30 42 … N By Using User Defined Method
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of elements in the series. - Prompt the user to enter a number as value of
n
. - Then call a user defined method
printSeries()
by passingn
as parameter. - Inside method declare an integer variable say ‘
result
’ and initialize it to 0 - Let declare another integer variable say ‘
add
’ and initialize it to 2 - 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 = 0; int add=2; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { result +=add; add+=2; System.out.print(" "+result); } } }
Output: Enter the number of elements 9 0 2 6 12 20 30 42 56 72
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: