In Java The nth Harmonic number is the sum of the reciprocal

In Java: The n^th Harmonic number is the sum of the reciprocals of the first n natural numbers:
H(n) = 1+ 1/2 + 1/3 +1/4 +... +1/n

Write a recursive method and an accompanying main method to compute the n^th Harmonic number.

I have tried but get a blank and would really apreciate a fully explained code.

Solution

<Harmonic.java>

import java.io.*;

public class Harmonic {

public static void main(String [] args) throws IOException {

double result;

System.out.print(\"Enter the value of n : \"); //user input for the limit of n value

int x = Integer.parseInt(s);

Harmonic h = new Harmonic(); //create an object of the Harmonic class which we defined above.

result = h.harmonic(x); //using this object, call the harmonic() method defined inside Class.

System.out.print(\"The value of nth harmonic number is : \" + result); //print the nth harmonic number value.

}

}

In Java: The n^th Harmonic number is the sum of the reciprocals of the first n natural numbers: H(n) = 1+ 1/2 + 1/3 +1/4 +... +1/n Write a recursive method and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site