Given the following recursive function: public static int recur(int num) { if (n
ID: 2246860 • Letter: G
Question
Given the following recursive function: public static int recur(int num) { if (num > 100) return num: else return recur((num * 2) + 3): } If the first call from within the main method is: int result = recur(4): What value will be passed as the num argument on the third call to recur (given that the call from within main is the first call)? ________. Using the recur function definition and call from the previous question, how many times will the recur function be recursively called before returning a result to the main method?Explanation / Answer
Question 22 :]
( Given that the call from the main is first call )
First Call - num value 4 will be passed which is provided from main function call
Second Call - 4 is not greater then 100 . Hence if condition will failed and else will be executed according to which ((4 * 2) + 3 ) or 11 will be passed as num value
Third Call - 11 is not greater then 100 . Hence if condition will failed and else will be executed according to which ((11 * 2) + 3 ) or 25 will be passed as num value
So answer is 25
Question 23 :]
NOTE : 1 - In this question call from main will not be included in recursive function call counting (bcoz it's given)
2 - Recursion will terminate / return only when if statement become true or we can say num > 100
when function called num = 4
if ( 4 > 100) is false hence else condition will not execute else part will execute which is recursive call to recur
CALL 1 : num = ( (4 * 2) + 3 ) = 11
CALL 2 : num = ((11 * 2) + 3 ) = 25
CALL 3 : num = ((25 * 2) + 3 ) = 53
CALL 4 : num = ((53 * 2) + 3 ) = 109
Now since num = 109 > 100 is true hence if statement will execute and if statement is returning current value of num hence recursive function call will halt.
So ans = 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.