Hi can check my code below and see whether it has meet the following, if not kindly assist and correct it for me.  a). Recursive function in Java that accepts an integer as input and returns 1 + 1/2 + 3 + 1/4 + ...(n or 1/n). b). The answer is the sum of the odd integers from 1 to n plus the sum of the reciprocals of the even integers. Thank you.   public class Main {public static double sum(int n) {if (n <= 0) {return 0;} else if (n % 2 == 0) {return 1.0 / n + sum(n - 1);} else {return n + sum(n - 1);}}public static void main(String[] args) {System.out.println(sum(5));}}

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
100%
Hi can check my code below and see whether it has meet the following, if not kindly assist and correct it for me. 
a). Recursive function in Java that accepts an integer as input and returns 1 + 1/2 + 3 + 1/4 + ...(n or 1/n).
b). The answer is the sum of the odd integers from 1 to n plus the sum of the reciprocals of the even integers. Thank you.
 
public class Main {
public static double sum(int n) {
if (n <= 0) {
return 0;
} else if (n % 2 == 0) {
return 1.0 / n + sum(n - 1);
} else {
return n + sum(n - 1);
}
}

public static void main(String[] args) {
System.out.println(sum(5));
}
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 1 steps

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning