Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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) calls fun(6) and fun(7).
  • fun(7) calls fun(5) and fun(6).
  • fun(6) calls fun(4) and fun(5).
  • fun(5) calls fun(3) and fun(4).
  • fun(4) calls fun(2) and fun(3).
  • fun(3) calls fun(1) and fun(2).
  • fun(2) calls fun(0) and fun(1).
  • fun(1) and fun(0) both return 1.

If you add up all the return values, you get 34. So, the output of the program is 34.

This problem has been solved

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);}

1/3

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.