Control Flow
Java program To Find The Nth Fibonacci Number
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:
To find the Nth Fibonacci number
A java program to find the Nth Fibonacci number.
Java Code
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();
}
}Output 1
Enter the value of n: 69
The Nth Fibonacci Number is 72723460248141Output 2
Enter the value of n: 12
The Nth Fibonacci Number is 89Java Program to Find GCD and LCM
Learn to calculate Greatest Common Divisor (GCD) and Least Common Multiple (LCM) using various algorithms including Euclidean method, recursion, and array operations.
Java Program to Reverse a Number
Learn different methods to reverse a number in Java with complete code examples and explanations.
