Advertisement
vertc

21-02

Feb 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. long long ciag (int n) {
  6.     if (n==1) return 0;
  7.     if (n==2) return 1;
  8.     if (n>2) return (ciag(n-1)) + (ciag(n-2));
  9. }
  10.  
  11. int main () {
  12.     int n;
  13.     cout<<"Podaj liczbe: ";
  14.     cin>>n;
  15.     cout<<"Wartosc w ciagu Fibonacciego dla "<<n<<"-tej liczby to: "<<ciag(n)<<endl;
  16.     return 0;
  17. }
  18.  
  19. -------------------------------------------------------------------------------------
  20.  
  21. #include <iostream>
  22.  
  23. using namespace std;
  24.  
  25. void ciag (int n) {
  26.     long long a=0, b=1;
  27.     for (int i=1; i<n; i++) {
  28.         b += a;
  29.         a = b-a;   
  30.     }
  31.     cout<<a;
  32. }
  33.  
  34. int main () {
  35.     int n;
  36.     cout<<"Podaj pozycje w ciagu: ";
  37.     cin>>n;
  38.     cout<<"Wartosc w ciagu dla "<<n<<" jest: ";
  39.     ciag(n);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement