Answers for "recursion fibonacci sequence"

0

recursive fibonacci

def fibo2(n):
        if n ==1 or n==0:
            return 1
        else :
            n = n+fibo2(n-1) 
            return n 
    print(fibo2(n1))
Posted by: Guest on July-15-2021
0

fibonacci sequence c recursion

int fib(int n)
{
    if (n <= 1)
        return n;
    return fib(n-1) + fib(n-2);
}
Posted by: Guest on November-12-2021
0

fibonacci sequence without recursion

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * Java Program to print Fibonacci series without using recursion.
 * input : 10
 * output : 0 1 1 2 3 5 8 13 21 34 55 
 *
 * @author WINDOWS 8
 */

public class FibonacciSeriesWithoutRecursion {

    public static void main(String args[]) {
     
        // printing first 10 numbers of Fibonacci series
        fibonacci(10);
     
    }
     
 
    public static void fibonacci(int number){
        for(int i=0; i <= number; i++){
            System.out.print(getFibonacci(i) + " ");
        }
    }
  
    /**
     * This function return nth Fibonacci number in Java
     * @param n
     * @return nth number in Fibonacci series
     */
    public static int getFibonacci(int n){
      
        if (n == 0) {
            return 0;
        }
        
        if (n == 1){
            return 1;
        }

        int first = 0;
        int second = 1;
        int nth = 1;

        for (int i = 2; i <= n; i++) {
            nth = first + second;
            first = second;
            second = nth;
        }
        return nth;
    }
  
}

Output : 0 1 1 2 3 5 8 13 21 34 55
Posted by: Guest on November-23-2020

Code answers related to "recursion fibonacci sequence"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language