GATE CSE 2018 Q31: Number of recursive calls counted in a C program
- Programming and Data Structures
- 1 mark
- NAT
- No negative marking
Syllabus topics: Recursion; Programming in C
Question
Consider the following 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*c*c);
}
}
int main (){
calc(4, 81);
printf ("%d", counter);
}The output of this program is _____.
Try it: numerical answer: type the value
Numerical answer. No negative marking.
Show answer and explanation
Answer: 4 (official GATE 2018 final answer key)
Concept used: Tracing recursion: counter is incremented once per call, and b is divided by 3 on each call until it reaches 3.
First step: List the calls made: calc(4, 81), then the value of b in each following call until the base case b == 3.
Full step-by-step solution
Loading…
Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.
More questions on Recursion
Every PYQ on this topic →Share this question
Source: official GATE 2018 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15