hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    Consider the following C code: #include<stdio.h> int temp = 0; int fun(int x, int y) { int z; temp++; if (y==3) return (x*x*x)
    Question



    Consider the following C code:
    #include<stdio.h>
    int temp = 0;
    int fun(int x, int y)
    {
    int z; temp++;
    if (y==3) return (x*x*x);
    else{
    z = fun(x, y/3);
    return (z*z*z);
    }
    }
    int main ()
    {
    fun(4, 81);
    printf(“%d”, temp);
    }
    What will be the output of the above code?

    A.

    3

    B.

    4

    C.

    5

    D.

    6

    Correct option is B


    The program involves recursive function calls, where each call increments a global variable temp. The value of temp represents the count of recursive calls made until a specific condition is met. Understanding recursion depth is crucial for predicting temp's final value.
    Information Booster:
    1. Initial Call: fun(4, 81) is called, which increments temp by 1.
    2. Recursive Calls:
    · 1st Call: fun(4, 81) calls fun(4, 27), incrementing temp to 2.
    · 2nd Call: fun(4, 27) calls fun(4, 9), incrementing temp to 3.
    · 3rd Call: fun(4, 9) calls fun(4, 3), incrementing temp to 4.
    3. Base Case: When y == 3, it returns x * x * x, terminating further recursion.
    Additional Knowledge:
    · Each recursive call reduces y by dividing it by 3.
    · The final output is based on the global variable temp, reflecting the number of times fun() was called.

    Free Tests

    Free
    Must Attempt

    Basics of Education: Pedagogy, Andragogy, and Hutagogy

    languageIcon English
    • pdpQsnIcon10 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon12 Mins
    languageIcon English
    Free
    Must Attempt

    UGC NET Paper 1 Mock Test 1

    languageIcon English
    • pdpQsnIcon50 Questions
    • pdpsheetsIcon100 Marks
    • timerIcon60 Mins
    languageIcon English
    Free
    Must Attempt

    Basics of Education: Pedagogy, Andragogy, and Hutagogy

    languageIcon English
    • pdpQsnIcon10 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon12 Mins
    languageIcon English

    Similar Questions

    test-prime-package

    Access ‘UGC NET Computer Science’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    354k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow