Home > Enterprise >  Step by step recursion
Step by step recursion

Time:04-13

int f(int x)
{
  if (x < 1)
    return 0;
  else  
    return f(x - 3)   x;
}

Can some one explain how it calculates the final result for f(10)=22? step by step please

CodePudding user response:

Working through this step by step is not difficult.

f(10)
f(10 - 3)   10
(f(7 - 3)   7)   10
((f(4 - 3)   4)   7)   10
(((f(1 - 3)   1)   4)   7)   10
(((0   1)   4)   7)   10
((1   4)   7)   10
(5   7)   10
12   10
22

CodePudding user response:

f(10) = f(7)   10 = f(4)   10   7 = f(1)   10   7   4 = f(-2)   10   7   4   1 = 0   10   7   4   1
  •  Tags:  
  • c
  • Related