fork download
  1. /*
  2. Compute the Fibonacci number
  3. Fn using procedural techniques
  4.  
  5. f(6) = f(6-1) + f(6-2) = f5 + f4 = 5 + 3 = 8
  6. */
  7.  
  8.  
  9. #include <iostream>
  10. #include <math.h>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. //float Binet(float n);
  16. int FibiProc (int n);
  17.  
  18. int main(){
  19. int userinput = 0;
  20. bool done = false;
  21.  
  22. cout.precision(15);
  23. cout<<"----------------------------------"<<endl;
  24. cout<<"Lets compute a Fibonacci Sequence!"<<endl;
  25. cout<<"----------------------------------"<<endl;
  26. cout<<endl<<"Please enter a integer:"<<endl<<"n = ";
  27. cin>>userinput;
  28. cout<<endl;
  29. if (userinput>60){
  30. cout<<"That number is to high for this calculator."<<endl<<"As the Fibonacci Sequence uses irrational numbers!!";
  31. cout<<endl<<endl;
  32. }else{
  33. cout<<"Using a Procedural Function the Fibonacci Sequence of F("<<userinput<<")"<<endl;
  34. cout<<endl<<endl;
  35. cout<<"F("<<userinput<<") = "<<FibiProc(userinput);
  36. cout<<endl<<endl;
  37. }
  38. cout<<endl<<endl<<endl<<"Please Close Console Window"<<endl;
  39. cin.ignore('\n', 1024);
  40. return(0);
  41.  
  42. }
  43.  
  44. int FibiProc(int n){
  45.  
  46. if (n==0 || n==1){
  47. return 1;
  48. }else{
  49. int result=1;
  50. int current=1;
  51. int last=1;
  52. for (int i=2; i<n; i++){
  53. result = current + last;
  54. last = current;
  55. current = result;
  56. }
  57. return(result);
  58. }
  59.  
  60.  
  61. }
Success #stdin #stdout 0.01s 2728KB
stdin
40
stdout
----------------------------------
Lets compute a Fibonacci Sequence!
----------------------------------

Please enter a integer:
n = 
Using a Procedural Function the Fibonacci Sequence of F(40)


F(40) = 102334155




Please Close Console Window