fibonacci series - recursive summation

前端 未结 6 1333
天命终不由人
天命终不由人 2021-02-09 14:45

Ok, I initially wrote a simple code to return the Fibonacci number from the series based on the user input..

n=5 will produce 3..

static int fibonacci(in         


        
6条回答
  •  深忆病人
    2021-02-09 15:29

    another approach to print Fibonacci series using recursive function.

    #include 
    
    // 0 1 1 2 3 5 8 13...
    //
    
    void fibb (int idx, int curr = 0, int next = 0)
    {
            std::cout << curr << ", ";
            if(!idx) return;
            if(curr == 0) {
                    curr = 1;
                    fibb(--idx, curr, next);
                    return;
            }
            next += curr;
            fibb(--idx, next, curr);
    }
    
    
    int main()
    {
            fibb(10);
    }
    

提交回复
热议问题