Search
 
SCRIPT & CODE EXAMPLE
 

CPP

find nth fibonacci number

// a dynamic programming approach for generating any number of fibonacci number
#include<bits/stdc++.h>
using namespace std;
 
long long int save[100]; // declare any sized array
long long int fibo(int n){
  if(n==0) return 0;
  if(n==1) return 1;
  if(save[n]!=-1) return save[n]; //if save[n] holds any value that means we have already calculated it and can return it to recursion tree
  save[n]=fibo(n-1)+fibo(n-2); // if it come tp this line that means I don't know what is the value of it
  return save[n];
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(save,-1,sizeof save);
  cout<<fibo(2)<<'
';

  return 0;
}
Comment

nth fibonacci number

def fib(n):
    if(n<0):
        print("Invalid input")
    elif(n==0):
        return 0
    elif(n==1) or (n==2):
        return 1
    else:
        return fib(n-1)+fib(n-2)
n=int(input())
print(fib(n))
Comment

nth fibonacci number

// Find N-th fibonacci number using recursion

#include <bits/stdc++.h>
using namespace std;

int fibo(int n)
{
    if (n == 1)
        return 1;
    if (n == 2)
        return 1;
    return fibo(n - 1) + fibo(n - 2);
}

int main()
{
    int n, T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        cout << fibo(n) << endl;
    }

    return 0;
}
Comment

nth fibonacci number

function nthFibonacci(n, cn = 0, val = 0, pval = 0) {
    if (n == cn) return pval;
    if (val == 0) val++;
    return nthFibonacci(n, cn + 1, val + pval, val);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: C++ Pointers to Structure 
Cpp :: cpp substring 
Cpp :: c++ threadpool 
Cpp :: max circular subarray sum gfg practice 
Cpp :: c++ initialize size of 3d vector 
Cpp :: c++ comment 
Cpp :: virtual function in c++ 
Cpp :: char at in c++ 
Cpp :: is there garbage collection in c++ 
Cpp :: inverted triangle c++ 
Cpp :: cpprestsdk send file 
Cpp :: codeforces problem 1030A solution 
Cpp :: flipperRSocketResponder.cpp command failed react native 
Cpp :: Restart the computer in c++ after the default time (30) seconds. (Windows) 
Cpp :: varint index 
Cpp :: c++ solver online free 
Cpp :: Road sign detection and recognition by OpenCV in c 
Cpp :: Variabili globali c++ 
Cpp :: c++ program to convert fahrenheit to kelvin 
Cpp :: . Shell sort in c++ 
Cpp :: 10^18 data type in c++ 
Cpp :: C++ Join thread 
Cpp :: create dynamic variable c++ 
Cpp :: sfml time set 
Cpp :: 1822. Sign of the Product of an Array leetcode 
Cpp :: distinct numbers cses 
Cpp :: c++ regex to validate indian phone number pattern 
Cpp :: what is stdarg.h used for 
Cpp :: c++ first index 0 or 1 
Cpp :: find maximum contiguous Sub arrays 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =