Question

In: Computer Science

In Java: The n^th Harmonic number is the sum of the reciprocals of the first n...

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.

Solutions

Expert Solution

<Harmonic.java>

import java.io.*;

public class Harmonic {

public static double harmonic(int n) { // create a double type method
    if(n == 1) {
        return 1.0;                    
    } else {
        return (1.0 / n) + harmonic(n - 1); //using recursion method to calculate every next 
                                            //value for harmonnic number & adding them all.
    }
}

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

BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
String s= inp.readLine();

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.

}

}


Related Solutions

The n th Triangle Problem Write a code for finding the n th triangle number of...
The n th Triangle Problem Write a code for finding the n th triangle number of triangle sequences: 1, 3, 6, 10, ..., n. That is, your code should accept an integer number, which indicates the triangle levels, and returns how many dots we need to form a triangle with respect to the given level. For example, consider the Fig 1. For n = 3 (can be also written as T3), your code should returns 6. Provide a single program...
A triangular number is the sum of the n natural numbers from 1 to n. For...
A triangular number is the sum of the n natural numbers from 1 to n. For example: The triangular number for 3 is 1 + 2 + 3 = 6 The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Provide the Java code to compute the sum, average, maximum number and minimum number if I...
Provide the Java code to compute the sum, average, maximum number and minimum number if I have a string sentence of double numbers. Assume that the length of the string sentence is not known. It can be of any length. To split a string based on the comma character use the following. String sentence = "A,B,C,D,E"; String[] stringsArray = receivedSentence.split(","); Then stringsArray is an array of five elements such that: stringsArray[0] = 'A' stringsArray[1] = 'B' stringsArray[2] = 'C' stringsArray[3]...
Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum...
Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum of the positive divisors of n (as in the notes). (a) Evaluate τ (1500) and σ(8!). (b) Verify that τ (n) = τ (n + 1) = τ (n + 2) = τ (n + 3) holds for n = 3655 and 4503. (c) When n = 14, n = 206 and n = 957, show that σ(n) = σ(n + 1).
Java language please Problem There are N houses for sale. The i-th house costs Ai dollars...
Java language please Problem There are N houses for sale. The i-th house costs Ai dollars to buy. You have a budget of B dollars to spend. What is the maximum number of houses you can buy? Input The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a single line containing the two integers N and B. The second line contains N integers. The i-th integer is...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) =...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) = 12 +22 +32 +...+n2 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n squares if n = 1 return 1 else return S(n − 1) + n* n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward non-recursive algorithm...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first n prime numbers. For example, P(1) = 2 and P(3) = 10, since the first three prime numbers are 2, 3 and 5, respectively. Write a program to determine the value of the function P(n) for different values of n. The first few prime sums are 2, 5, 10, 17, 28, 41, 58 and 77. Input The first line of the input file contains...
For the following exercises, use the formula for the sum of the first n terms of each geometric sequence, and then state the indicated sum.
For the following exercises, use the formula for the sum of the first n terms of each geometric sequence, and then state the indicated sum.
Use the formula for the sum of the first n terms of an arithmetic series to find the sum of the first eleven terms of the arithmetic series 2.5, 4, 5.5, … .
Use the formula for the sum of the first n terms of an arithmetic series to find the sum of the first eleven terms of the arithmetic series 2.5, 4, 5.5, … .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT