Advertisement
Combreal

fibonacci01.cpp

Jul 4th, 2020
1,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string.h>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. int fibonacci(int row);
  9.  
  10. int main(void)
  11. {
  12.   int userValue = 0;
  13.   cout<<"Enter desired row of fibonaci : ";
  14.   cin>>userValue;
  15.   cout<<"\n"<<fibonacci(userValue)<<endl;
  16.   return 0;  
  17. }
  18.  
  19. int fibonacci(int row)
  20. {
  21.   int result = 0;
  22.   int nmtwo = 0;
  23.   int nmone = 0;
  24.   if(row == 0)
  25.     {
  26.       cout<<"Finbonacci's row start at one"<<endl;
  27.       //return 0;
  28.       exit(0);
  29.     }
  30.   for (int i=0;i<=row;i++)
  31.     {
  32.       int tmp = nmtwo;
  33.       nmtwo = nmone;
  34.       nmone = nmone + tmp;
  35.       if(i == row)
  36.       {
  37.          result = nmone;                                                                                                                 }
  38.       if (i == 1)
  39.     {
  40.       nmone = 1;
  41.       result = 0;
  42.     }
  43.       else if (i == 2)
  44.     {
  45.             nmtwo = 0;
  46.         nmone = 1;
  47.         result = 1;
  48.          }
  49.       }
  50.   return result;
  51. }
  52.  
  53. //sudo gcc main.cpp -lstdc++ -g -w -o fibonacci
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement