In the previous article we have discussed about Java Program to Print Fibonacci Number Series
In this article we are going to see how to print the harmonic series by using Java programming language.
Java Program to Print the Harmonic Series
Harmonic series is the inverse of arithmetic progression.
The terms in a harmonic progression can be denoted as below
h1 = 1/a, h2 = 1/(a+d), h3 = 1/(a+2d), h4 = 1/(a+3d), . . hn = 1/(a+nd).
Where,
- h = harmonic series
- a = arithmetic progression
- d = common difference between arithmetic progression
- n = nth term
Let’s see different ways to print harmonic series.
Method-1: Java Program to Print the Harmonic Series By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n
. - Declare an integer variable say ‘
a
’ which holds the 1st number of arithmetic progression. - Prompt the user to enter a number as value of
a
. - Declare an integer variable say ‘
d
’ which holds the common difference between arithmetic progression. - Prompt the user to enter a number as value of
d
. - Let declare a double variable say ‘
result
’ - Use a for loop
from i=1 to i<=n
where the loop is incremented by 1 - Inside the for loop we will find the value of
result = 1/(a+(i*d))
- 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 number of terms "); int n = s.nextInt(); System.out.println("Enter the value of a "); int a = s.nextInt(); System.out.println("Enter the value of common difference ‘d’ "); int d = s.nextInt(); double result; System.out.print("The harmonic series is "); //for loop to print the series for (int i = 1; i <= n; i++) { result = (double) 1/(a+(i*d)); System.out.print(result+" "); } } }
Output: Enter number of terms 5 Enter the value of a 2 Enter the value of common difference ‘d’ 1 The harmonic series is 0.3333333333333333 0.25 0.2 0.16666666666666666 0.14285714285714285
Method-2: Java Program to Print the Harmonic Series By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n
. - Declare an integer variable say ‘
a
’ which holds the 1st number of arithmetic progression. - Prompt the user to enter a number as value of
a
. - Declare an integer variable say ‘
d
’ which holds the common difference between arithmetic progression. - Prompt the user to enter a number as value of
d
. - Let declare a double variable say ‘
result
’ - Declare and initialize an integer variable
i=1
- Continue a while loop
till i<=n
, wherei
is incremented by 1. - Inside the while loop we will find the value of
result = 1/(a+(i*d))
- 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 number of terms "); int n = s.nextInt(); System.out.println("Enter the value of a "); int a = s.nextInt(); System.out.println("Enter the value of common difference ‘d’ "); int d = s.nextInt(); double result; System.out.print("The harmonic series is "); //while loop to print the series int i=1; while(i<=n) { result = (double) 1/(a+(i*d)); System.out.print(result+" "); i++; } } }
Output: Enter number of terms 5 Enter the value of a 3 Enter the value of common difference ‘d’ 1 The harmonic series is 0.25 0.2 0.16666666666666666 0.14285714285714285 0.125
Method-3: Java Program to Print the Harmonic Series By Using User Defined Method
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n
. - Declare an integer variable say ‘
a
’ which holds the 1st number of arithmetic progression. - Prompt the user to enter a number as value of
a
. - Declare an integer variable say ‘
d
’ which holds the common difference between arithmetic progression. - Prompt the user to enter a number as value of
d
. - Then call a user defined method say
printSeries()
by passingn
,a
andd
as parameter. - Inside method declare a double variable say ‘
result
’ - Use a for loop
from i=1 to i<=n
where the loop is incremented by 1 - Inside the for loop we will find the value of
result = 1/(a+(i*d))
- 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 number of terms "); int n = s.nextInt(); System.out.println("Enter the value of a "); int a = s.nextInt(); System.out.println("Enter the value of common difference ‘d’ "); int d = s.nextInt(); // calling printSeries method to print the series printSeries(n,a,d); } //printSeries metthod to print the series public static void printSeries(int n, int a, int d) { double result; System.out.print("The harmonic series is "); //for loop to print the series for (int i = 1; i <= n; i++) { result = (double) 1/(a+(i*d)); System.out.print(result+" "); } } }
Output: Enter number of terms 5 Enter the value of a 5 Enter the value of common difference ‘d’ 2 The harmonic series is 0.14285714285714285 0.1111111111111111 0.09090909090909091 0.07692307692307693 0.06666666666666667
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.
Related Java Programs: