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 72723460248141
Output 2
Enter the value of n: 12
The Nth Fibonacci Number is 89
Java Program to Calculate Factorial of a Number
Learn to calculate factorial of a number in Java using loops and recursion. Complete program with examples showing both iterative and recursive approaches.
Java Program to calculate quotient and reminder
In this program you'll learn, How you can calculate the quotient and reminder of a number (divident) by dividing a number (divisor)