Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to code the Fibonacci Sequence using simple iterative loops java

// Here's how to get the nth Fibonacci number Code in Java using a for loop:

import java.util.*;
public class fibonacci{
    public static void main(String args[]){
        int n,k;
        Scanner snr= new Scanner(System.in);
        n=snr.nextInt();
        snr.close();
        int array[]=new int[n];
        // The space used here is O(N)
        array[0]=0;
        array[1]=1;
        for(k=2;k<n;k++)array[k]=array[k-1]+array[k-2];
        // The array is traversed only once so time complexity is O(N)
        System.out.println("Nth number in Fibonacci series is "+array[n-1]);
    }
}
Comment

How to code the Fibonacci Sequence using simple iterative loops in java

// Here's how to get the nth Fibonacci number code in Java using a while loop:

import java.util.*;
public class fibonacci{
    public static void main(String args[]){
        int n,k;
        Scanner snr= new Scanner(System.in);
        n=snr.nextInt();
        snr.close();
        int array[]=new int[n];
        // The space used here is O(N)
        array[0]=0;
        array[1]=1;
        k=2;
        while(k<n)
            array[k]=array[k-1]+array[k-2];
            k++;
        System.out.println("Nth number in Fibonacci series is "+array[n-1]);
    }
    // The array is traversed only once so the time complexity is O(N)
}
Comment

PREVIOUS NEXT
Code Example
Java :: constructors in java 
Java :: Mirror Inverse Program in java 
Java :: java print line 
Java :: class java 
Java :: java Generate parentheses all combinations 
Java :: java secureRandom certain range 
Java :: validate data type in request body spring validation 
Java :: paysimple 
Java :: clear datepicker javafx 
Java :: kotlin dependency injection 
Java :: domain validation test spring boot 
Java :: change size bitmapfont 
Java :: fill a 2d array java 
Java :: string stack in java 
Java :: null pointer exception during registering user to the firebase 
Java :: spring mvc aop transaction management 
Java :: how to write a java program for printing child or adult in java 
Java :: change replication factor hadoop cluster command 
Java :: java escribir ventana grafica 
Java :: Java Get float, double and String Input 
Java :: Java @SafeVarargs annotation 
Java :: quadratic program 
Java :: LayerRenderer 
Java :: contoh object dalam oop java 
Java :: convert kotlin to java online 
Java :: java polymorphism nedir 
Java :: java pair class 
Java :: Java find duplicate items 
Java :: result set methods 
Java :: is string a primitive data type/use of getclass() 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =