Java program to calculate harmonic value – In this article, we will learn the multiple ways to calculate the harmonic value in Java Programming.
Suitable examples and sample programs have been included in order to make you understand simply. The compiler has also been added so that you can execute the programs yourself.
The methods used in this article are as follows:
- Using Standard Method
- Using Scanner Class
- Using Static Method
- Using Separate Class
Harmonic Value or the Harmonic Number is the sum of the reciprocals up to a given value.
TWe can see that this is the general formula to calculate the Harmonic Value for the first n numbers.
If n = 4, then
Hn = 1 + 1/2 + 1/3 + 1/4 = 2.083333
Harmonic Value – Using Standard Method
To determine the harmonic value, our first step is to get input of the number until which we want to find the harmonic value. The input value can be assigned to a variable (n) in the code itself and our task in to find the harmonic value from 1 to this number.
In order to store the harmonic value, we take another variable (sum). Then we iterate using a loop till the number and keep adding all the reciprocal numbers as shown below:
for( i=1;i<=n;i++) { sum=sum+(1/i); }
This value sum, is our resultant output harmonic value, which can be displayed in the output screen as desired.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Harmonic { public static void main(String arg[]) { double n=10,i; double sum=0; for( i=1;i<=n;i++) { sum=sum+(1/i); } System.out.println("Harmonic value = "+sum); } } |
Output:
1 |
Harmonic value = 2.9289682539682538 |
Using Scanner Class
In the standard method, the input for the program was given in the code itself. In future, if we want to try the same code for a different input then, it’ll require us to go the code and make the necessary changes in the input. This is not a convenient method.
Instead, we could make use of a special class in Java named the Scanner class. By making use of this class, inputs for any primitive datatype like int, float, double, char, etc., can be read at runtime itself in the console screen by the user.
Thereby, making use of this scanner class, we’ll read our required input a number (n) of type double and the calculate the sum of reciprocals from 1 to the number (n).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Harmonic { public static void main(String arg[]) { double n,i; double sum=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextDouble(); for( i=1;i<=n;i++) { sum=sum+(1/i); } System.out.println("Harmonic value = "+sum); } } |
Output1:
1 2 3 |
Enter a number 2 Harmonic value = 1.5 |
Using Static Method
In both the previous methods, we have seen that the entire code is written inside main method only.
The problem that arises with this is that, when we would like to make use of the same logic again in the code, we’ll have to rewrite the same set of statements.
Instead of doing so, if we write the logic used within another static method, it saves a lot of time, reduces number of lines as we’ll just have to call this method and pass necessary parameters wherever required. This way a part of code becomes reusable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class Harmonic { public static void main(String arg[]) { double a; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); a=sc.nextDouble(); double h=harmonicCal(a); System.out.println("Harmonic value = "+h); } static double harmonicCal(double n) { double sum=0; for(double i=1;i<=n;i++) { sum=sum+(1/i); } return sum; } } |
Output1:
1 2 3 |
Enter a number 15 Harmonic value = 3.3182289932289937 |
Using Separate Class
In all the above method, the entire code is written inside the same class.
The problem that arises with this is that, the code when written like that is not that readable. It could be difficult to find a particular part of code. Instead, we can split the code and make use of another separate class.
To do so, we’ll split the code into two parts. The first part is the main method class which is responsible for all the input output operations same as the previous methods while, the second part is another separate class(Hvalue) which will contain a constructor or method having the same logic as discussed above.
This will be invoked with the help of an object created referencing it (h) within the main method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class Hvalue { double s=0; Hvalue(double n) { for(double i=1;i<=n;i++) { s=s+(1/i); } } } class Harmonic { public static void main(String arg[]) { double a; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); a=sc.nextDouble( ); Hvalue h=new Hvalue(a); System.out.println("Harmonic value = "+h.s); } } |