Untitled

 avatar
unknown
plain_text
2 years ago
814 B
5
Indexable
#include <iostream>
using namespace std;

// Function to calculate factorial of a number
int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    else {
        return n * factorial(n - 1);
    }
}

// Function to display the Fibonacci series
void fibonacci(int n) {
    static int n1 = 0, n2 = 1, n3;
    if (n > 0) {
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        cout << n3 << " ";
        fibonacci(n - 1);
    }
}

int main() {
    int num;
    cout << "Enter a positive integer: ";
    cin >> num;

    cout << "Factorial of " << num << " is " << factorial(num) << endl;

    cout << "Fibonacci series up to " << num << " terms: ";
    if (num >= 1) {
        cout << "0 ";
    }
    if (num >= 2) {
        cout << "1 ";
    }
    fibonacci(num - 2);

    return 0;
}