What is the output printed by the following program? #include<stdio.h> int…

GATE · 2005 · IT

What is the output printed by the following program?

#include<stdio.h>
int f(int n, int k)
{
    if (n == 0)
        return 0;
    else if (n % 2)
        return f(n/2, 2*k) + k;
    else return f(n/2, 2*k) - k;
}
int main ()
{
    printf("%d", f(20, 1));
    return 0;
}

  1. A.

    5

  2. B.

    8

  3. C.

    9

  4. D.

    20

Attempted by 112 students.

Sign up free to check your answer

Sign up free

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…