ritesh1340

Fibonacci Recursion.cpp

Dec 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. // Generating the 'n' th fibonacci number
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define ll long long
  7. ll ans=0;
  8.  
  9. ll fib( ll n )
  10. {
  11.     if ( n==1 ) return 0;
  12.     if ( n==2 ) return 1;
  13.  
  14.     ans=fib(n-1); + fib(n-2);
  15.     return ans;
  16. }
  17.  
  18.  
  19. void solve()
  20. {
  21.     int n;
  22.     cout<<"Enter the term number : ";
  23.     cin>>n;
  24.  
  25.     ans=fib(n);
  26.  
  27.     cout<<"'n'th Fibonacci number is : "<<ans<<"\n";
  28. }
  29.  
  30. int main()
  31. {
  32.     int t;
  33.     cout<<"Enter number of test cases : ";
  34.     cin>>t;
  35.  
  36.     while ( t-- ) solve();
  37.  
  38.     return 0;
  39. }
Add Comment
Please, Sign In to add comment