Write C++ program to generate nth fibonacci term using recursion


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n)
  5. {
  6. if((n==1)||(n==0))
  7. {
  8. return(n);
  9. }
  10. else
  11. {
  12. return(fibonacci(n-1)+fibonacci(n-2));
  13. }
  14. }
  15.  
  16. int main()
  17. {
  18. int n,i=0;
  19. cout<<"Input the number of terms for Fibonacci Series:";
  20. cin>>n;
  21. cout<<"\nFibonacci Series is as follows\n";
  22.  
  23. while(i<n)
  24. {
  25. cout<<" "<<fibonacci(i);
  26. i++;
  27. }
  28.  
  29. return 0;
  30. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts