Program: 131
Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term.
Following are the first few terms of the Fibonacci sequence:
1 1 2 3 5 8 12 21 34 55 89
How to use recursion to get the Fibonacci series.
#include<stdio.h> int fibo(int num); void main() { int num,c=0,i; printf("Enter number: "); scanf("%d", &num); printf("Fibonacci Series:\n"); for(i=1;i<=num;i++) { printf("%d\n", fibo(c)); c++; } } int fibo(int num) { if(num==0) { return 0; } else if(num==1) { return 1; } //fibonacci = 1 1 2 3 5 8 // where n = (n-1) + (n-2) else { return (fibo(num-1)+fibo(num-2)); } }
Output:
Enter number: 25 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Leave a Comment