Skip to main content

Posts

Showing posts with the label Write a program to generate Fibonacci series up to n terms using recursive function.

Write a program to generate Fibonacci series up to n terms using recursive function.

 Here's a C++ program to generate the Fibonacci series up to n terms using a recursive function: #include <iostream> using namespace std; int fibonacci(int n) {     if (n <= 1) {         return n;     }     return fibonacci(n-1) + fibonacci(n-2); } int main() {     int n;     cout << "Enter the number of terms: ";     cin >> n;     cout << "Fibonacci Series: ";     for (int i = 0; i < n; i++) {         cout << fibonacci(i) << " ";     }     return 0; } Output: Enter the number of terms: 7 Fibonacci Series: 0 1 1 2 3 5 8 Explanation: The program defines a function fibonacci() that takes an integer argument n and returns the n-th term in the Fibonacci series. The function uses recursion to calculate the Fibonacci number. If n is 0 or 1, the function returns n itself. Otherwise, it returns the sum of the p...