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 :: Java alt f4 
Java :: declaration of an array in java 
Java :: min in java 
Java :: hash table implementation java 
Java :: math.min java 
Java :: java enum 
Java :: advantages of exception handling in java 
Java :: how to subtract localdatetime in java 
Java :: asscending linkedlist remove duplicates valuesjava 
Java :: how to stop activity from another activity 
Java :: class declaration in java 
Java :: convert list of dto to map java 
Java :: quarkus skip test 
Java :: how to get request json web token in next js 
Java :: Calling User-Defined Method in Java 
Java :: spring boot swagger ui 401 
Java :: elif java 
Java :: hashmap in java 
Java :: actuator spring boot 
Java :: set jcombobox index java 
Java :: set matrix zeros programming creek 
Java :: Java Hashmap Add Elements 
Java :: java lambda expressions 
Java :: how to create object of abstract class in java 
Java :: Java Converting double into an int 
Java :: cant change button color when app run android studio 
Java :: replace all these caracters in string java 
Java :: android studio clock bar change color programmatically 
Java :: lcm of two large positive integers java 
Java :: how to pass a float between activities in android studio 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =