This Program-code finds the time required to find the nth Fibonacci no. using recursive algorithm.
This program takes no input and outputs .xls file in which the nth Fibonacci no. with the time to compute that is written
Fibonacci no. with Recursion
- #include<stdio.h>
- #include<time.h>
- int fibonacci(int i);
- int main()
- {
- time_t t1,t2;
- int i,no,fib;
- FILE *fp;
- fp=fopen("fibo.xls","w");
- if(fp==NULL)
- {
- printf("ERROR IN FILE OPENNING\n");
- exit(1);
- }
- fprintf(fp,"No\tFibonacci\ttime\n");
- for(i=1;i<=40;i++)
- {
- fprintf(fp,"%d\t",i);
- (void) time(&t1);
- fprintf(fp,"%d",fibonacci(i));
- (void) time(&t2);
- fprintf(fp,"\t%ld\n",(long)(t2-t1));
- }
- printf("fibo.xls file has been created.\n");
- getch();
- return 0;
- }
- int fibonacci(int i)
- {
- int x;
- if(i==1)
- return 0;
- else if(i==2)
- return 1;
- else
- {
- x=fibonacci(i-1)+fibonacci(i-2);
- return x;
- }
- }
You can take a look to the xls file I’ve got in my Laptop.
For further details contact me.
0 Responses to Fibonacci Series Time Comparison for Recursive Algorithm