Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to get the nth Fibonacci number code in Java using recursion

import java.util.*;
public class fibonacci{
    public static void main(String args[]){
        int n;
        Scanner snr= new Scanner(System.in);
        n=snr.nextInt();
        snr.close();
        System.out.println(fib(n)); 
//Printing number in Fibonacci series
    }
    public static int fib(int n){
        if(n==0){
            return 0;
        }
        // Base cases return itself 0 and 1
        else if(n==1){
            return 1;
        }
        else{
            return fib(n-1)+fib(n-2);
            // Recursive calls
        }
    }
}
Comment

How to get the nth Fibonacci number code in Java using recursion with memoization

import java.util.*;
public class fibonacci{
    public static int fib(int n){
        if(n==1){
            return array[0];
        }
        // base cases
        if(n==2){
            return array[1];
        }
        else{
            array[n-1] = fib(n-1) + fib(n-2);
            return (array [n-1]);
        }
    }
    public static void main(String args[]){
        int n;
        Scanner snr= new Scanner(System.in);
        n=snr.nextInt();
        snr.close();
        array[0]=0;
        array[1]=1;
        System.out.println(fib(n));
        // printing number in fibonacci series
    }
    static int array[]=new int[1000];
    // Declaring global array large enough
 }
Comment

PREVIOUS NEXT
Code Example
Java :: deifine an integer array in java 
Java :: change short video to byte array android 
Java :: Calendar ranges java 
Java :: how to convert infix to postfix and prefix in java 
Java :: neither bindingresult nor plain target object for bean name spring mvc 
Java :: mvn spring boot ends when closing vm 
Java :: integer parse int exceptions 
Java :: spring mvc project example 
Java :: java get year difference between two dates 
Java :: java how to make a parameter optional 
Java :: how to create Java ArrayList 
Java :: boolean 
Java :: horizontal recyclerview item width half of screen android 
Java :: android java show hide keyboard in AndroidManifest 
Java :: iterative inorder traversal 
Java :: arraylist remove name in java 
Java :: java session timeout 
Java :: java sub method 
Java :: java list tostring 
Java :: 7877777777777777777777777 
Java :: difference between two sets java 
Java :: instance of 
Java :: isWhiteSpace java 
Java :: measure view height android 
Java :: java string loop backwards 
Java :: abstract class java constructor 
Java :: what is java 
Java :: priority queue java 
Java :: java array quick sort 
Java :: how to close scanner in java 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =