OA. free
Free
AMI Core Computer Science Core Computer Science Medium

What is the output of the following C program?

AMI technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What is the output of the following C 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;
}
Choose one option.
Show answer & explanation
Answer: A. 12

Standard evaluation according to engineering and computational science principles.

Step-by-step Derivation:
Step 1: Analyze problem specification.
Step 2: Derive theoretical solution.
Step 3: Select corresponding answer option.