-Define H₁ = -Write a private static double method in Week4Main called harmonicl which is iterative, that takes in integer n as input, and returns H. - these are referred to as the harmonic numbers. -Write a private static double method in Week4Main called harmonicR which is recursive, that takes in integer n as input, that returns H₁.
Hello. I was wondering if an expert could help me with a
I will leave a screenshot of what it is asking.
Input: An integer n representing the desired harmonic number to be calculated.
Output: The harmonic number Hn.
1. Initialize n:
- Read an integer n from the user. This represents the desired harmonic number.
2. Calculate Harmonic (Iterative):
- Define a method harmonicI(n) to calculate Hn iteratively:
- Initialize a variable H to 0.0 to store the result.
- Loop i from 1 to n:
- Calculate 1.0 divided by i and add it to H.
- Return the value of H as the harmonic number Hn.
3. Calculate Harmonic (Recursive):
- Define a method harmonicR(n) to calculate Hn recursively:
- Check if n is equal to 1:
- If true, return 1.0 (base case).
- If n is greater than 1:
- Recursively call harmonicR with n - 1.
- Add 1.0 divided by n to the result of the recursive call.
- Return this updated result.
4. Print Results:
- Print the calculated harmonic number Hn for both the iterative and recursive methods.
5. End of Program.
Step by step
Solved in 4 steps with 2 images