What will be the output of the given program?int fun(int i){if(i==1 || i==0){return 1;}return fun(i-2)+fun(i-1);}void main(){int x=8;printf("%d", fun(x));}Options32303436
Question
What will be the output of the given program?int fun(int i){if(i==1 || i==0){return 1;}return fun(i-2)+fun(i-1);}void main(){int x=8;printf("%d", fun(x));}Options32303436
Solution
The output of the given program will be 34.
Here's the step by step explanation:
The function fun() is a recursive function that takes an integer i as an argument. If i is 1 or 0, it returns 1. Otherwise, it calls itself twice, once with i-2 and once with i-1, and returns the sum of these two calls.
In the main() function, fun() is called with x which is 8.
Here's how the recursion works:
fun(8)callsfun(6)andfun(7).fun(7)callsfun(5)andfun(6).fun(6)callsfun(4)andfun(5).fun(5)callsfun(3)andfun(4).fun(4)callsfun(2)andfun(3).fun(3)callsfun(1)andfun(2).fun(2)callsfun(0)andfun(1).fun(1)andfun(0)both return 1.
If you add up all the return values, you get 34. So, the output of the program is 34.
Similar Questions
What will be the output of the following program?#include<stdio.h>int main() { int fun(int); int i = fun(10); printf("%d\n", --i); return 0; } int fun(int i) { return (i++);}Select one:810119
What will be the output of the given code?123456789101112#include <stdio.h> void fun(int *ptr){ *ptr = 30;}int main(){ int y = 20; fun(&y); printf("%d", y); return 0;}
What will be the output of the following code?#include <stdio.h>int main() { int i = 0; for (; i < 5; ) { printf("%d ", i); i++; } return 0;}
What is the output of following program?# include <stdio.h>void fun(int x){ x = 30;}int main(){ int y = 20; fun(y); printf("%d", y); return 0;}a.30b.20c.Compile errord.Run error
What will be the output of the following code?int i;for (i = 0; i < 5; i++) { if (i == 3) { break; } printf("%d ", i);}
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.