In the previous article we have discussed about Java Program to Display Sum of Series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N
In this article we are going to see how to print the series 1.5 3.0 4.5 6.0 7.5 …N by using Java programming language.
Java Program to Print the Series 1.5 3.0 4.5 6.0 7.5 …N
On observing the pattern carefully, we can see
1st number starts from 1.5, and the next number is 1.5 added to the previous number, Where N is the number of terms.
Example:
1.5 1.5 + 1.5 = 3.0 3.0 + 1.5 = 4.5 4.5 + 1.5 = 6.0 6.0 + 1.5 = 7.5 So the series for N=5 is 1.5 3.0 4.5 6.0 7.5
Let’s see different ways to print the series 1.5 3.0 4.5 6.0 7.5 …N.
Method-1: Java Program to Print the Series 1.5 3.0 4.5 6.0 7.5 …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 double variable
result
and initialize it to 1.5. - Use a for loop
from i=1 to i<n
incremented by 1 - Inside loop we will increment the result with addition of 1.5 to the previous 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(); double result = 1.5; System.out.print(result); //for loop to print the series for (int i = 1; i < n; i++) { result +=1.5; System.out.print(" "+result); } } }
Output: Enter the Nth term “N” 5 1.5 3.0 4.5 6.0 7.5
Method-2: Java Program to Print the Series 1.5 3.0 4.5 6.0 7.5 …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
result
and initialize it to 1.5. - Declare and initialize an double variable
i=1
- Continue a while loop till
i<n
incremented by 1 - Inside loop we will increment the result with addition of 1.5 to the previous 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(); double result=1.5; System.out.print(result); int i=1; while(i<n) { result +=1.5; System.out.print(" "+result); i++; } } }
Output: Enter the Nth term “N” 7 1.5 3.0 4.5 6.0 7.5 9.0 10.5
Method-3: Java Program to Print the Series 1.5 3.0 4.5 6.0 7.5 …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
. - Let declare an double variable
result
and initialize it to 1.5. - Use a for loop
from i=1 to i<n
incremented by 1 - Inside loop we will increment the result with addition of 1.5 to the previous 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); } //user defined method printSeries() to print the series public static void printSeries(int n) { double result = 1.5; System.out.print(result); //for loop to print the series for (int i = 1; i <n; i++) { result +=1.5; System.out.print(" "+result); } } }
Output: Enter the value of Nth term 'N' 9 1.5 3.0 4.5 6.0 7.5 9.0 10.5 12.0 13.5
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Related Java Programs: