Please complete with comments and pseudo code (in Java) This is answered here: h
ID: 3867051 • Letter: P
Question
Please complete with comments and pseudo code (in Java)
This is answered here: https://www.chegg.com/homework-help/questions-and-answers/14-code-recursive-solution-exercise-11-d-provide-driver-program-test-solution-11-d-give-ba-q16480081
If it is correct please just add in the pseudo code.
Code a recursive solution for Exercise 11(d), and provide a driver program to test your solution.
Here's Q11 d:
Q11: Give the base case, reduced problem, and general solution of the recursive algorithm for:
d. The sum of the integers from a to b, a > b.
Explanation / Answer
public class RecursiveSum {
/**
* @param args
*/
public static void main(String[] args) {
RecursiveSum recursiveSum = new RecursiveSum();
System.out.println("sum of 5 to 7:" + recursiveSum.recursion(5, 7));
System.out.println("sum of 9 to 7:" + recursiveSum.recursion(9, 7));
System.out.println("sum of 9 to 9:" + recursiveSum.recursion(9, 9));
}
/**
* method to find the sum from a to b using recursion
*
* @param a
* @param b
* @return
*/
public int recursion(int a, int b) {
if (a > b)
return 0;
else if (a == b) // base case
return a;
else
return a + recursion(a + 1, b);
}
}
OUTPUT:
sum of 5 to 7:18
sum of 9 to 7:0
sum of 9 to 9:9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.