I'm supposed to create a method to find the nth harmonic number. Say the use asks for the third harmonic number, I want it to return the sum of 1, 1/2, and 1/3. I am required to solve this recursively and I have difficulty thinking that way. I am nonplussed as to why this code does not work. The System.out.prints I do indicate that n is decrementing as intended but the sum of harmonic numbers is not accumulating. I've included the mini test program so if you want to run this you can go ahead. Thanks! This is my first post here btw.
public class HarmonicTest { public static void main(String[] args) { System.out.println(Harmonic.getHarmonic(3)); //getHarmonic(3)); } } import java.util.*; public class Harmonic { public static double getHarmonic(int n) { if(n>0) { return(getHarm(n, 0)); } else throw new InputMismatchException("The input for n must be greater than 0."); } public static double getHarm(int n, double harmonic) { harmonic = harmonic + (1/n); System.out.println("Int is: " + n); System.out.println("Harmonic is: " + harmonic); if(n == 1) { return harmonic; } else return(getHarm((n-1), harmonic)); } }