1. Write a program that includes the recursive method and then call the method to calculate Sum = XXXXXXXXXX. Hint : refer to #1 of M7 Code Examples. 2. Write a program that includes the recursive...

1 answer below »

1. Write a program that includes the recursive method and then call the method to calculate
Sum
= 50 + 51 ... + 100.





Hint: refer to #1 of M7 Code Examples.






2. Write a program that includes the recursive method that will sum up the fraction numbers:
S(n)
= 1/2 + 2/3 + ... + (n-1)/n, and then call the method to calculate S (10) = 1/2 + 2/3 + ... + 9/10. Display the result to one decimal place. Hint:
S(n) = S(n-1) + (n-1)/n.





Hint: refer to #3 of M7 Code Examples.






3. Write a program that includes the recursive method and then call the method to print all even numbers from 10 to 20.





Hint: refer to #7 of M7 Code Examples.




Module 7 Code Examples Example 1 Write the application that includes the recursive method to sum up the numbers from 1 to n: Sum(n) = 1 + 2 + ... + n, and then call the method to calculate Sum (100) = 1 + 2 + ... + 100. Hint: Sum(n)=Sum(n-1)+n. import java.util.Scanner; public class Code7_1 { public static void main(String[] args) { int s = sum (100); System.out.println("The result is: "+s); System.out.println(); } //----------- public static int sum (int n) { if (n==1) return 1; else return sum(n-1)+n; } } Screen shots of the program and execution result are as follows. Example 2 Write the application that includes the recursive method to calculate the factorial of n: n! = 1 * 2 * ... * n, and then call the method to calculate 10! = 1 * 2 * ... * 10. Hint: f(n)=n!=f(n-1)*n. import java.util.Scanner; public class Code7_2 { public static void main(String[] args) { int f = f (10); System.out.println("The result is: "+f); System.out.println(); } //----------- public static int f (int n) { if (n==1) return 1; else return f(n-1)*n; } } Screen shots of the program and execution result are as follows. Example 3 Write the application that includes the recursive method to sum up the numbers from 1/1, 1/2, … to i/n: S(n) = 1/1 + 1/2 + ... + 1/n, and then call the method to calculate S (10) = 1/1 + 1/2 + ... + 1/10. Display the result to one decimal place. Hint: S(n)=S(n-1)+1/n. import java.util.Scanner; import java.text.DecimalFormat; public class Code7_3 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("0.0"); double s = s (10); System.out.println("The result is: " + f.format(s)); } //------------ public static double s (int n) { if (n==1) return 1; else return 1./(double)n + s(n-1); } } Screen shots of the program and execution result are as follows. Example 4 Based on the last question, read the integer n from the keyboard, pass it through the same method, and output the result to the monitor. Use a loop, so the code could run continuously. import java.util.Scanner; import java.text.DecimalFormat; public class Code7_4 { public static void main(String[] args) { Scanner input = new Scanner(System.in); DecimalFormat f = new DecimalFormat("0.0"); int n; double s; System.out.print("Input n: "); n = input.nextInt(); while(n>0) { s = s (n); System.out.println("The result is: "+f.format(s)); System.out.print("Input n: "); n = input.nextInt(); } } //------------ public static double s (int n) { if (n==1) return 1; else return 1./(double)n + s(n-1); } } Screen shots of the program in TextPad and execution results including the output file are as follows. Example 5 Given the following program, what would be shown in the monitor when it is executed? import java.util.Scanner; public class Code7_5 { public static void main(String[] args) { CountUp(0); System.out.println("\n\n"); } //----------- public static void CountUp ( int num ) { if (num>10) System.out.println(); else { System.out.print(num+" "); CountUp(num+1); } } } It output should be: 0 1 2 3 4 5 6 7 8 9 10. In the recursive method, when 0 for num is passed, it shows in the monitor, so as it increases as 1, 2, …, 10, that will be displayed in the monitor. When sum is 11, it display nothing. Screen shots of the program in TextPad and execution results are as follows. Example 6 Given the following program, what would be shown in the monitor when it is executed? import java.util.Scanner; public class Code7_6 { public static void main(String[] args) { CountUp(0); System.out.println("\n\n"); } //---------- public static void CountUp ( int num ) { if (num>10) System.out.println(); else { CountUp(num+1); System.out.print(num+" "); } } } It output should be: 10 9 8 7 6 5 4 3 2 1. In the recursive method, when 0 for num is passed, it will hide under the next calling and not show in the monitor, so as it increases as 1, 2, …, 10, that will hide and not display in the monitor. When sum is 11, it display nothing, and then the last hiding number (10) is displayed, then 9, 8, …, 0. Screen shots of the program in TextPad and execution results are as follows. Example 7 Based on Example 5, write the recursion form of the code to print all odd numbers from 1 to 10, that is, 1, 3, 7, and 9. Note: we could modify the program of Example 5, starting from 1, not 0; and increasing by 2, not 1, shown below. import java.util.Scanner; public class Code7_7 { public static void main(String[] args) { CountUp(1); System.out.println("\n\n"); } //---------- public static void CountUp ( int num ) { if (num>10) System.out.println(); else { System.out.print(num+" "); CountUp(num+2); } } } Screen shots of the program in TextPad and execution results including the output file are as follows. Example 8 Based on Example 7, write the recursion form of the code to print all odd numbers reversely from 10 to 20, that is, 19, 17, 15, 13, and 11. Note: we could modify the program of Example 7, starting from 11, not 1; ending 20, not 10; and reverse the order of output statement with recursive call, shown below. import java.util.Scanner; public class Code7_8 { public static void main(String[] args) { CountUp(11); System.out.println("\n\n"); } //---------- public static void CountUp ( int num ) { if (num>20) System.out.println(); else { CountUp(num+2); System.out.print(num+" "); } } } Screen shots of the program in TextPad and execution results including the output file are as follows. Page 1 of 13 Page 7 of 13
Answered 1 days AfterAug 16, 2021

Answer To: 1. Write a program that includes the recursive method and then call the method to calculate Sum =...

Arun Shankar answered on Aug 18 2021
152 Votes
public class Main
{
    public static int sum(int arg)
    {
        if(arg == 100)
            return 100;
        retur
n arg + sum(arg + 1);
    }
    
    public static double second_fn(int n)
    {
        if(n == 1)
            return 0;
        return second_fn(n-1) + ((double)(n - 1)/ (double)n);
    }
    
    public static void print_even(int...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here