Program to compute a specified Fibonacci number using recursion – modified

#include <stdio.h>
 long ncalls=0;
 
 long fib(int);
 
 int main()
 {
    int n;
 
    printf("Enter the value : ");
    scanf("%d",&n);
    printf("nFib(%d) = %ld.",n,fib(n));
    printf("nNo of calls to the function : %dn",ncalls);
    return 0;
 }
 
 long fib(int n)
 {
    ncalls++;
    if(n > 2)
        return (fib(n-1) + fib(n-2));
    else
        return 1;
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.