// 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]);
}
}
// 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)
}