C program to generate Fibonacci Series using recursion.
“Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.” ― Albert Einstein
Program 1:-
#include <stdio.h> #include <conio.h> void fibo(long,long, long, long); int main(){ long n; printf("\n Enter number of fibonacci series : "); scanf(" %ld", &n); fibo(0,1,1,n); getch(); } void fibo(long a, long b, long i, long n){ long c; if (i<=n){ printf("\n %ld", a); c=a+b; a=b; b=c; fibo(a,b,i+1,n); } }
Program 2:-
#include <stdio.h> #include <conio.h> long fibo(long); int main(){ long n,f,i; printf("\n Enter number of fibonacci series : "); scanf(" %l*d", &n); for(i=1;i<=n;i++){ f=fibo(i); printf("\n %ld", f); } getch(); } long fibo(long n){ long c; if (n==1){ return(0); } else if (n==2){ return(1); } else { return(fibo(n-1) + fibo(n-2)); } }
Comments
Post a Comment