18.6 (Sum series) Write a recursive method to compute the following series: m(i)
ID: 3691906 • Letter: 1
Question
18.6 (Sum series) Write a recursive method to compute the following series: m(i) = 1/2 + 2/3 + ... + i/i + 1 Write a test program that displays m(i) for i = 1, 2, . . ., 10. 18.25 (String permutation) Write a recursive method to print all the permutations of a string. For example, for the string abc, the permuation is: abc acb bac bca cab cba (Hint: Define the following two methods. The second is a helper method.) public static void displayPermutation(String s) public static void displayPermutation(String s1, String s2) The first method simply invokes displayPermutation(" ", s). The second method uses a loop to move a character from s2 to s1 and recursively invokes it with a new s1 and s2. The base case is that s2 is empty and prints s1 to the console. Write a test program that prompts the user to enter a string and displays all its permutations.Explanation / Answer
import java.text.DecimalFormat;
public class Series
{
public static double m(int i)
{
double sigma = 0;
for (i = i;i>0;i--)
{
sigma += ((double)i)/(i+1);
}
return sigma;
}
public static void main(String args[])
{
System.out.println("i m(i) = ======");
for (int i=1;i<=20;i++)
System.out.println(i+" "+(new DecimalFormat("#.####").format(m(i))));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.