What will be the output of the following program?12345678910111213#include <stdio.h> int ren(int n) { if (n == 6) return n; else return 2*ren(n+1);}int main() { printf("%d", ren(2)); return 0;}
Question
What will be the output of the following program?12345678910111213#include <stdio.h> int ren(int n) { if (n == 6) return n; else return 2*ren(n+1);}int main() { printf("%d", ren(2)); return 0;}
Solution
The output of the program will be 64. Here's the step-by-step explanation:
- The function
ren(int n)is called withnequal to 2. - Since
nis not equal to 6, theelsepart of theifstatement is executed. This callsren(n+1), which isren(3), and multiplies the result by 2. - This process repeats for
ren(4),ren(5), andren(6). - When
ren(6)is called,nis equal to 6, so the function returns 6. - This return value is then propagated back up the call stack, being multiplied by 2 at each level. So
ren(5)returns2*6 = 12,ren(4)returns2*12 = 24,ren(3)returns2*24 = 48, and finallyren(2)returns2*48 = 96. - This final return value of 96 is then printed by the
printfstatement in themainfunction.
Similar Questions
What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}
What will be the output of the following program?
What will be the output of the following program?12345678910111213#include <stdio.h> int ren(int n) { if (n == 6) return n; else return 2*ren(n+1);}int main() { printf("%d", ren(2)); return 0;}
What will be the output of the following program?12345678910#include <stdio.h> int main() { int a[10]; *a = 7; *(a+1) = 12; int p = *(a+1) / *a; printf("%d", p); return 0;}
What will be the output of the following code?1234567891011121314151617#include <stdio.h>int main() { int i = 0, product = 10;start: if (i < 4) { product *= (i + 1); i++; if (i != 2) goto start; } printf("%d", product); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.