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 :: java naming convention acronyms 
Java :: leftView 
Java :: java random number generator 6 
Java :: franchiseRulesTemp 
Java :: Java search() Method 
Java :: <selector xmlns:android="http://schemas.android.com/apk/res/android<item android;drawable="@drawable/bluebtn" android: state_enabled="false"/ 
Java :: list all managed beans in spring 
Java :: priority queue is empty java 
Java :: last element array java 
Java :: Java Default capacity and load factor 
Java :: intellij evaluate expression 
Java :: connect 2 package in android 
Java :: android gradle plugin requires java 11 problem 
Java :: how to write to a txt file in java in the end 
Java :: grunt registertask multiple 
Java :: create new instance of class java 
Java :: google pass api integration in java 
Java :: how to check if something exists in an sql column java 
Java :: fibonacci numbers using recursion in java 
Java :: Java instanceof in Interface 
Java :: android java onUpgrade() 
Java :: unmappable character java 
Java :: How to Register a Custom Auto-Configuration? 
Java :: connectionpool in jdbc 
Java :: @javax.annotation.Generated error java stub 
Java :: java get first node in a list 
Java :: Convert Java File to Kotlin File 
Java :: Run the ls command in the terminal to see the uncompiled .java file 
Java :: java param.ExStyle |= 0x08000000; 
Java :: steps to accomplish jdbc connection in java 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =