This code takes the value of N from user and displays the Nth number of the Fibonacci Series.
N is a variable which inputs a integer value using System.in.
To understand this example, you should have the knowledge of the following Java programming topics:
A java program to find the Nth Fibonacci number.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
long n, term1=0, term2=1, i, fib=1 ;
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter the value of n: ") ;
n = sc.nextInt() ;
if (n == 1) {
fib = term1;
}
else if (n == 2) {
fib = term2;
}
else {
for (i = 3 ; i <= n ; i ++)
{
fib = term1 + term2;
term1 = term2;
term2 = fib;
}
}
System.out.println("The Nth Fibonacci Number is " + fib) ;
sc.close();
}
}
Enter the value of n: 69
The Nth Fibonacci Number is 72723460248141
Enter the value of n: 12
The Nth Fibonacci Number is 89