In the previous article we have discussed about Java Program to print Series 11 22 33 44 55 … N5
In this article we are going to see how to print the series 5 6 9 14 21 … N by using Java programming language.
Java Program to Print the Series 5 6 9 14 21 … N
On observing the pattern carefully, we can see
1st number starts from 5, then the next number is the sum of previous number + new number
Where new number starts from 1 and incremented by 2 in term.
Example:
5 | 6 | 9 | 14 | 21 | 30 | … | N |
+1 +3 +5 +7 +9
Let’s see different ways to print the series 5 6 9 14 21 … N
Method-1: Java Program to Print the Series 5 6 9 14 21 … 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 5 - Let declare another integer variable say ‘
add
’ 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+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 = 5; int add = 1; 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 5 6 9 14 21
Method-1: Java Program to Print the Series 5 6 9 14 21 … 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 5 - Let declare another integer variable say ‘
add
’ 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 while loop 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=5; int add = 1; 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” 7 5 6 9 14 21 30 41
Method-3: Java Program to Print the Series 5 6 9 14 21 … 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 5 - Declare another integer variable say ‘
add
’ 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+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 = 5, add = 1; 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' 9 5 6 9 14 21 30 41 54 69
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: