What value would the following function return for the input x = 95? Function…

1998

What value would the following function return for the input x = 95?

Function fun (x: integer): integer;

Begin

If x > 100 then fun = x − 10

Else fun := fun(fun(x + 11))

End;

  1. A.

    89

  2. B.

    90

  3. C.

    91

  4. D.

    92

Attempted by 65 students.

Show answer & explanation

Correct answer: C

This recursive function behaves similarly to the McCarthy 91 function.

The function definition is:

If x > 100
    fun(x) = x − 10
Else
    fun(x) = fun(fun(x + 11))

Given:

x = 95

Since:

95 ≤ 100

We apply:

fun(95) = fun(fun(106))

Now:

fun(106) = 106 − 10 = 96

So:

fun(95) = fun(96)

Again:

96 ≤ 100

Thus:

fun(96) = fun(fun(107))

Now:

fun(107) = 107 − 10 = 97

So:

fun(96) = fun(97)

Continuing this recursive process eventually gives:

fun(x) = 91

for all values:

x ≤ 100

Therefore:

fun(95) = 91

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir