Tip:A–D to answerE for explanationV for videoS to reveal answer
AMU 2026 — Computer PYQ
AMU | Computer | 2026
What is the output of this C program?
#include<stdio.h>
int counter = 0;
int calc(int a, int b){
int c;
counter ++;
if (b==3)
return (a*a*a);
else {
c= calc(a, b/3);
return (c cc);
}
}
int main () {
calc(4,81);
printf("%d", counter);
}
Choose the correct answer:
A.
4
(Correct Answer)
B.
5
C.
6
D.
10
Correct Answer:
4
Explanation
The program tracks how many times the recursive function calc(int a, int b) is called using a global variable named counter. Every time calc() is entered, counter increments by 1 via the line counter++;.
Let us trace the execution calls starting from the main() function:
Initial Call from main():
calc(4, 81) is called.
counter increments from 0 to 1.
Check base condition: Is b<mark>3? Here 81</mark>3 is False.
It enters the else block and triggers a recursive call: calc(4, 81/3) which is calc(4, 27).
Second Recursive Call:
calc(4, 27) is executed.
counter increments from 1 to 2.
Check base condition: Is 27==3? False.
Triggers the next recursive call: calc(4, 27/3) which is calc(4, 9).
Third Recursive Call:
calc(4, 9) is executed.
counter increments from 2 to 3.
Check base condition: Is 9==3? False.
Triggers the next recursive call: calc(4, 9/3) which is calc(4, 3).
Fourth Recursive Call (Base Case Reached):
calc(4, 3) is executed.
counter increments from 3 to 4.
Check base condition: Is 3==3? True.
The condition matches, so it returns (4×4×4)=64 directly without calling itself further.
Unwinding the Stack:
The functions return their values back up the call stack sequentially, but no more increments to counter happen.
Control returns to main().
Final Print:
printf("%d", counter); prints the final state of the global variable counter, which is 4.
AMU 2026 Computer PYQ — What is the output of this C program? #include<stdio.h> int count… | Mathem Solvex | Mathem Solvex
Explanation
The program tracks how many times the recursive function calc(int a, int b) is called using a global variable named counter. Every time calc() is entered, counter increments by 1 via the line counter++;.
Let us trace the execution calls starting from the main() function:
Initial Call from main():
calc(4, 81) is called.
counter increments from 0 to 1.
Check base condition: Is b<mark>3? Here 81</mark>3 is False.
It enters the else block and triggers a recursive call: calc(4, 81/3) which is calc(4, 27).
Second Recursive Call:
calc(4, 27) is executed.
counter increments from 1 to 2.
Check base condition: Is 27==3? False.
Triggers the next recursive call: calc(4, 27/3) which is calc(4, 9).
Third Recursive Call:
calc(4, 9) is executed.
counter increments from 2 to 3.
Check base condition: Is 9==3? False.
Triggers the next recursive call: calc(4, 9/3) which is calc(4, 3).
Fourth Recursive Call (Base Case Reached):
calc(4, 3) is executed.
counter increments from 3 to 4.
Check base condition: Is 3==3? True.
The condition matches, so it returns (4×4×4)=64 directly without calling itself further.
Unwinding the Stack:
The functions return their values back up the call stack sequentially, but no more increments to counter happen.
Control returns to main().
Final Print:
printf("%d", counter); prints the final state of the global variable counter, which is 4.