Advertisement
Rakibul_Ahasan

Fibonacci Series

Jun 18th, 2022
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int fib(int n)
  5. {
  6.     // Stop condition
  7.     if (n == 0)
  8.         return 0;
  9.  
  10.     // Stop condition
  11.     if (n == 1 || n == 2)
  12.         return 1;
  13.  
  14.     // Recursion function
  15.     else
  16.         return (fib(n - 1) + fib(n - 2));
  17. }
  18.  
  19. int main()
  20. {
  21.     int n = 5;
  22.     cout<<"Fibonacci series of 5 numbers is: ";
  23.     for (int i = 0; i < n; i++){
  24.         cout<<fib(i)<<" ";
  25.     }
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement