Advertisement
KatarzynaBracha

Fibonacci

Mar 17th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. long int fibonacci(int n)
  5. {
  6.     if (n==1||n==2)
  7.         return 1;
  8.     else if(n>2)
  9.         return fibonacci(n-1)+fibonacci(n-2);
  10.     else
  11.         return -1;
  12. }
  13. int main()
  14. {
  15.     int n;
  16.     cout << "Ktory wyraz ciagu fibonacciego chcesz wyznaczyc: " ;
  17.       if(!(cin>>n))
  18.     {
  19.     cout <<"Incorrect input"<<endl;
  20.     return 1;
  21.     }
  22.         if(n<0)
  23.     {
  24.         cout <<"Incorrect input data";
  25.         return 2;
  26.     }
  27.  
  28.     cout<<n<<" wyraz ciagu fibonacciego wynosi:"<<fibonacci(n);
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement