Pages

Related Posts with Thumbnails
Fibonacci Series Time Comparison for Recursive Algorithm >> Codes-N-Tricks
Content feed Comments Feed

Fibonacci Series Time Comparison for Recursive Algorithm

Posted by Soham Friday, October 1, 2010

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
  1. #include<stdio.h>
  2. #include<time.h>
  3.  
  4. int fibonacci(int i);
  5.  
  6. int main()
  7. {
  8. time_t t1,t2;
  9. int i,no,fib;
  10. FILE *fp;
  11.  
  12. fp=fopen("fibo.xls","w");
  13. if(fp==NULL)
  14. {
  15.     printf("ERROR IN FILE OPENNING\n");
  16.     exit(1);
  17. }
  18.  
  19.   
  20. fprintf(fp,"No\tFibonacci\ttime\n");
  21. for(i=1;i<=40;i++)
  22. {
  23.     fprintf(fp,"%d\t",i);
  24.     (void) time(&t1);
  25.     fprintf(fp,"%d",fibonacci(i));
  26.     (void) time(&t2);
  27.     fprintf(fp,"\t%ld\n",(long)(t2-t1));
  28. }
  29. printf("fibo.xls file has been created.\n");
  30. getch();
  31. return 0;
  32. }
  33.  
  34. int fibonacci(int i)
  35. {
  36. int x;
  37. if(i==1)
  38. return 0;
  39. else if(i==2)
  40. return 1;
  41. else
  42. {
  43.   x=fibonacci(i-1)+fibonacci(i-2);
  44.   return x;
  45. }
  46. }

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

Post a Comment

SyntaxHighlighter

E-Mail Subscription

Enter your email address:

Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
Delivered by FeedBurner

Share with Orkut

Loading

Recent Posts

About Us