# Fibonacci Series Program in Java

What is Fibonacci Series in Java?

Fibonacci series are the numbers in the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21….. The series in the Fibonacci sequence is equal to the sum of the previous two terms. The Fibonacci sequence’s first two terms are 0 and 1 respectively.

Mathematically, we can denote it as:

Fn = Fn-1 + Fn-2

Where, Fn denotes the nth term of Fibonacci series.

The first two terms of this series are considered to be:
F0 = 0 (Zeroth term of Fibonacci sequence)
F1 = 1 (First term of Fibonacci sequence)

Now, by using the above two values we can easily calculate all other terms of Fibonacci series as follows :

F2 = F1 + F0 = 0 + 1 = 1
F3 = F2 + F1 = 1 + 1 = 2
F4 = F3 + F2 = 2 + 1 = 3

Series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ……

Problem Description

Write a Java Program to generates fibonacci series.

Problem Solution

In fibonacci series the first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

There are several ways to print fibonacci series in Java language. Let’s take a detailed look at all the approaches to display fibonacci series in Java

Method 1: Fibonacci Series in Java using For Loop

In this method, we use a for loop to generate n fibonacci series.

Program/Source Code

Here is the source code of the Java Program to Generate Fibonacci Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

```/*
* Fibonacci Series in Java using For Loop
*/

import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}```
Program Explanation

1. Enter the number of terms you want as an input.
2. The main class is named Fibonacci and declares integer variables n, a, b, and c.
3. The program generates a Fibonacci series up to n using a for loop.
4. For each iteration of the for loop, the current value of a is printed and then a, b, and c are updated according to the formula c = a + b.
5. When the for loop finishes, the program prints the Fibonacci series.

Runtime Test Cases

In this case, entering “5” as the limit to generate the fibonacci series.

```Enter value of n: 5
Fibonacci Series: 0 1 1 2 3```

Method 2: Fibonacci Series Program in Java using While Loop

In this method, we use a while loop to generate n fibonacci series.

Program/Source Code

Here is the source code of the Java Program to Generate Fibonacci Numbers using while loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

```/*
* Fibonacci Series in Java using While Loop
*/

import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1, i = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
while(i<=n)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
i++;
}
}
}```
Program Explanation

1. The main class is named Fibonacci and declares integer variables n, a, b, c, and i.
2. The program generates a Fibonacci series up to n using a while loop.
3. For each iteration of the while loop, the current value of a is printed and then a, b, and c are updated according to the formula c = a + b.
4, The loop continues as long as i is less than or equal to n, with i incrementing by 1 each iteration.
5. When the loop completes, the program prints the Fibonacci series

Runtime Test Cases

In this case, entering “8” as the value to generate the fibonacci series.

```Enter value of n: 8
Fibonacci Series: 0 1 1 2 3 5 8 13```

Method 3: Java Program to Implement Efficient O(log n) Fibonacci Generator

This is a program to generate nth fibonacci number with O(log n) complexity.

Program/Source Code

Here is the source code of the Java Program to Implement Efficient O(log n) Fibonacci generator . The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

```/*
* Java Program to Implement Efficient O(log n) Fibonacci generator
*/

import java.util.Scanner;
import java.math.BigInteger;

/** Class FibonacciGenerator **/
public class FibonacciGenerator
{
/** function to generate nth fibonacci number **/
public void genFib(long n)
{
BigInteger arr1[][] = {{BigInteger.ONE, BigInteger.ONE},
{BigInteger.ONE, BigInteger.ZERO}};
if (n == 0)
System.out.println("\nFirst Fibonacci number = 0");
else
{
power(arr1, n - 1);
System.out.println("\n"+ n +" th Fibonacci number = "+ arr1[0][0]);
}
}
/** function raise matrix to power n recursively **/
private void power(BigInteger arr1[][], long n)
{
if (n == 0 || n == 1)
return;
BigInteger arr2[][] = {{BigInteger.ONE, BigInteger.ONE},
{BigInteger.ONE, BigInteger.ZERO}};
power(arr1, n / 2);
multiply(arr1, arr1);
if (n % 2 != 0)
multiply(arr1, arr2);
}
/** function to multiply two 2 d matrices **/
private void multiply(BigInteger arr1[][], BigInteger arr2[][])
{
arr1[0][0] = x;
arr1[0][1] = y;
arr1[1][0] = z;
arr1[1][1] = w;
}
/** Main function **/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Efficient Fibonacci Generator\n");
System.out.println("Enter number n to find nth fibonacci number\n");
long n = scan.nextLong();
FibonacciGenerator fg = new FibonacciGenerator();
fg.genFib(n);
}
}```
Program Explanation

1. This program calculates the nth Fibonacci number using a matrix-based algorithm and the BigInteger class for handling large numbers.
2. The program is implemented as a class called FibonacciGenerator that has three methods: genFib, power, and multiply.
3. The genFib method uses the matrix-based algorithm to calculate the nth Fibonacci number.
4. The power method recursively raises a matrix to a power, while the multiply method multiplies two 2D matrices.
5. The main method prompts the user to enter the value of n and calls the genFib method to generate the nth Fibonacci number.

Runtime Test Cases

In this case, enter “1000” as the limit for generating the Fibonacci series.

```
Efficient Fibonacci Generator

Enter number n to find nth fibonacci number

1000

1000 th Fibonacci number = 43466557686937456435688527675040625802564660517371780
40248172908953655541794905189040387984007925516929592259308032263477520968962323
9873322471161642996440906533187938298969649928516003704476137795166849228875```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]