edited by
663 views
0 votes
0 votes
rec(int x)
{
    static int f;
    if(x==1)
    return 1;
    else
    f+=x*rec(x-1);
    return(f);
}

the value returned by rec(5) is_____________

edited by

2 Answers

Best answer
2 votes
2 votes
initially  f should be initialized to 0

so f=0

function f=f+x*rec(x-1)

rec(1)=1

f(2)=0+2*rec(1)=2

f(3)=2+3*rec(2)=8

f(4)=8+4*rec(3)=40

f(5)=40+5*rec(4)=240

s0 f returns 240
selected by
3 votes
3 votes
rec(5)-> f=f+(5*rec(4))=240

rec(4)->f=f+(4*rec(3))=40

rec(3)->f=f+(3*rec(2))=8

rec(2)->f=f+(2*rec(1))=2

rec(1)=1

so answer of rec(5) is 240

Related questions

0 votes
0 votes
1 answer
1
SSR17 asked Feb 29
204 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how
2 votes
2 votes
1 answer
4
rupamsardar asked Aug 30, 2023
466 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...