Write a recursive program to find nth fibonacci number.

This answer is restricted. Please login to view the answer of this question.

Login Now

Recursive program to find nth fibonacci number:

#include <stdio.h>

int fibonacci(int);

int main(){
    int numbers, i;

    printf("How many numbers? ");
    scanf("%d", &numbers);

    printf("Fibonacci series:\n");

    for (i = 0; i < numbers; i++){
        printf("%d\t", fibonacci(i));
    }

    return 0;
}

int fibonacci(int n){
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return (fibonacci(n - 1) + fibonacci(n - 2));
}

The output of above program is

How many numbers? 10
Fibonacci series:
0       1       1       2       3       5       8       13      21      34
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .