tags:

views:

124

answers:

1

how the stack would look like for the following program if I give input as 5.

#include <stdio.h>

int fibonacci(int number)
{
  int retval;
  if (0 == number){
    return 0;
  }
  if (1 == number){
    return 1;
  }
  return(fibonacci(number-1) + fibonacci(number-2));
}

int main()
{
  int number = 0;
  int fibvalue = 1;
  while (1){
    printf("please enter the number\n");
    scanf("%d", &number);
    fibvalue = fibonacci(number);
    printf("computed fibonacci value %d\n", fibvalue);
  }
  return 1;
}

also give me links where i can learn about it

+1  A: 

Use a debugger, for example, GDB.

Shameless plug - take a look at my GDB intro presentation at New York City BSD User Group - there are plenty of examples of Fibonacci stack traces there.

Nikolai N Fetissov