In the previous article we have discussed about Java Program to print Series 3 6 11 18 27 … N
In this article we are going to see how to print the series 7, 10, 8, 11, 9, 12, … N by using Java programming language.
Java Program to Print the Series 7, 10, 8, 11, 9, 12, … N
On observing the pattern carefully, we can see
1st number starts from 7, then the next number is 3 added with the previous number, and then the next number is 2 subtracted with the previous number.
Example:
7 | 10 | 8 | 11 | 9 | 12 | …… | N |
+3 -2 +3 -2 +3
Let’s see different ways to print the series 7, 10, 8, 11, 9, 12, … N
Method-1: Java Program to Print the Series 7, 10, 8, 11, 9, 12, … 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 7 and print it as first term. - 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 using if-else condition If
i
is odd, then we will add 3 with the result Else ifi
is even, then we will subtract 2 with the result - 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 = 7; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { if(i%2 != 0) result+=3; else result-=2; System.out.print(" "+result); } } }
Output: Enter the Nth term “N” 5 7 10 8 11 9
Method-2: Java Program to Print the Series 7, 10, 8, 11, 9, 12, … 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 7 and print it as first term. - 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 using if-else condition If
i
is odd, then we will add 3 with the result Else ifi
is even, then we will subtract 2 with the result - 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=7; System.out.print(result); int i=1; while(i<=n-1) { if(i%2 != 0) result+=3; else result-=2; System.out.print(" "+result); i++; } } }
Output: Enter the Nth term “N” 7 7 10 8 11 9 12 10
Method-3: Java Program to Print the Series 7, 10, 8, 11, 9, 12, … 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
. - Then call a user defined method say
printSeries()
by passingn
as parameter. - Inside method declare an integer variable say ‘
result
’ and initialize it to 7 and print it as first term. - 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 using if-else condition If
i
is odd, then we will add 3 with the result Else ifi
is even, then we will subtract 2 with the result - 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 = 7; System.out.print(result); //for loop to print the series for (int i = 1; i <= n-1; i++) { if(i%2 != 0) result+=3; else result-=2; System.out.print(" "+result); } } }
Output: Enter the value of Nth term 'N' 9 7 10 8 11 9 12 10 13 11
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: