155k views
2 votes
/* Write a method called fractionSum that accepts an integer parameter n and

* returns as a double the sum of the first n terms of the sequence:
* sum i = 1 to n of 1 / i
* You may assume that the parameter n is non-negative.
*/
public double fractionSum(int n) {
double sum = 0.0;
for(int i = 1; i <= n; i++)
sum += 1.0 / i;
return sum;
}

User Fledgling
by
5.2k points

1 Answer

3 votes

Answer:

See explanation

Step-by-step explanation:

You've already answered the question you posted.

The programming language is not stated. however, the syntax is close to java & c++ and there's a need for alteration on the program to make it run perfectly.

If it is Java,

change

public double fractionSum(int n) {

to

public static double fractionSum(int n) {

If it is C++, change

public double fractionSum(int n) {

to

double fractionSum(int n) {

To call the program from the main method, write:

Java:

System.out.print(fractionSum(n));

C++:

cout<<fractionSum(n);

User Mike Hedman
by
5.8k points