Pages

Friday, May 23, 2025

Nth Fibonacci (Code) Using Recursion in C++

 #include <iostream>

using namespace std;

int fibonacci(int n) {
    if(n == 0 || n == 1) {
        return n; // 0, 1
    }

    return fibonacci(n-1)+fibonacci(n-2);
}

int main() {
    cout << fibonacci(3) << endl;

    return 0;
}


OUTPUT:
2

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...