281 views
0 votes
0 votes

int main(){

                fun(4);

return 0;

}

int fun(int n){

                if(n>0){

                                int i=1;

                                for(;i<3;i++){

                                                fun(n-i);

                                                fun(n-i-1);

                                printf(“Hello”);

                                }

                }

return 0;

}

The number of times “Hello” will be printed _______

 

1 Answer

2 votes
2 votes

F(1) = for i=1, f(0) + f(-1) +pf() = 0+0+1=1 printf call

          for i=2, f(-1) + f(-2) + pf() = 0+0+1 =1 printf call

F(1) = 1+1 = 2

---------------------------------------------------------------------

F(2) = i=1, f(1) + f(0) +pf() = 2+0+1= 3 

          i=2, f(0) + f(-1) +pf() =0+0+1= 1 

F(2) = 3+1= 4

------------------------------------------------------------------------

F(3) = i=1, f(2) + f(1) + pf() = 4+2+1 = 7

          i=2, f(1) + f(0) +pf() = 2+0+1 =3

F(3) = 7+3 =10

-------------------------------------------------------------

F(4) = i=1, f(3) + f(2) + pf() = 10+4+1 =15

          i=2, f(2) + f(1) + pf() = 4+2+1 = 7

F(4) = 15+7 = 22 

 

Answer 22 times

edited by

No related questions found