This is the first solution to find the nTh number of the Fibonacci series.
import java.util.Scanner;
public class NthFibonacci {
public static void main(String[] args) {
Scanner number=new Scanner(System.in);
// To find the nTh number of Fibonacci series, read nTh value here.
System.out.println("Enter nTh number=");
int nTh=number.nextInt();
number.close();
//Call the function nthFib() with nth value as parameter
System.out.println("The "+nTh+"th value of the Fibonacci series is "+ NthFib(nTh));
}
private static int NthFib(int nthNum) {
int[] fib=new int[nthNum];
//Initialize the first two values in the Fibonacci series
fib[0]=0;
fib[1]=1;
//Evaluate each term in the series until the nthNum
for(int i=2;i<nthNum;i++)
fib[i]=fib[i-1]+fib[i-2];
return fib[nthNum-1];
}
}
OUTPUT:
Enter nTh number=
16
The 16th value of the Fibonacci series is 610

Leave a comment