Q. Predict the output of following codes.

(a)

def codo (n):

    if n== 0:

        print('Finally ')

    else:

              print(n)

              codo(n - 3)

codo(15)

 

(b)

def codo (n):

    if n== 0:

        print('Finally ')

    else:

              print(n)

              codo(n - 2)

codo(15)

 

(c)

def codo (n):

    if n== 0:

        print('Finally')

    else:

              print(n)

              codo(n - 2)

codo(10)

 

(d)

def codo (n):

    if n== 0:

        print('Finally')

    else:

              print(n)

              codo(n - 3)

codo(10)


Answer :-


(a)

Output:

15
12
9
6
3
Finally

>>>

Explaination :-


1. The function codo is defined with the parameter n.
2. The code checks if n is equal to 0 using the condition n == 0. If this condition is true, it means that the recursive call has reached the base case, and it prints the string 'Finally'. This serves as the termination condition for the recursive function.
3. If the condition in step 2 is false, meaning n is not equal to 0, it executes the else block.
4. It first prints the current value of n using the print(n) statement.
5. Then, it makes a recursive call to the codo function with the argument n - 3. This means that the function will call itself with the value of n reduced by 3. This recursion continues until the termination condition is met (when n becomes 0).
6. As an example, if you call codo(15), the initial value of n is 15. It is not equal to 0, so it goes to the else block. It prints 15 and then calls codo(15 - 3), which is equivalent to codo(12).
7. The recursive process continues until n becomes 0. At each step, the current value of n is printed.
8. Once n reaches 0, the recursion stops, and the program prints 'Finally'.
So, running codo(15) would output the following sequence of numbers: 15, 12, 9, 6, 3, and finally the string 'Finally'.


(b) It will give an error, error type (“RecursionError: maximum recursion depth exceeded while pickling an object”). Because there is no “base case”.


(c)

Output:

10
8
6
4
2
Finally

>>>

Explaination :-

1. Initially, the value of n is 10. It is not equal to 0, so it goes to the else block. It prints 10 and then calls codo(10 - 2), which is equivalent to codo(8).
2. The value of n is now 8. It is not equal to 0, so it prints 8 and makes a recursive call to codo(8 - 2), which is equivalent to codo(6).
3. The process continues. The value of n becomes 6, then 4, then 2, and for each value, it is printed and a recursive call is made with n reduced by 2.
4. Finally, when the value of n becomes 0, the base case is reached. It prints the string 'Finally', and the recursion stops.


(d) It will also give an error, error type (“RecursionError: maximum recursion depth exceeded while pickling an object”). Because there is no “base case”. 

4 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post