Home » Java Tutorial » Fibonacci Series in Java with Examples

# Fibonacci Series in Java with Examples

What is the Fibonacci series in java? The Fibonacci series is nothing but the addition of the previous two numbers. This series starts with 0 and 1 and then it goes on up to infinity. Let’s understand the logic behind the Fibonacci number with examples.

### The logic behind the Fibonacci series

Method of recursion is used to print the Fibonacci series. 0 and 1 are the first two elements of the Fibonacci series. Now let the sequence of the Fibonacci series as Fn

Fn = Fn-1 + Fn-2

Where n > 1

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,… is the Fibonacci series. As above mentioned, every number is an addition to the previous two numbers.

## Examples of Fibonacci series in java?

There are many ways to print the Fibonacci series. Examples of printing Fibonacci series using for and while loop, Fibonacci series using dynamic memory allocation, and Fibonacci series using recursion in java given below. Let’s understand it.

## 1. Fibonacci series using for loop

Take the number of terms as input to print the Fibonacci series up to N terms. Use N as an exit condition in for loop.

### Example

``````import java.util.Scanner;
public class Fibonacci_series {

public static void main(String[] args) {

int num1 =0, num2 =1; // first term of fibonacci series is 0 and second is 1
int num3;

Scanner obj = new Scanner(System.in); // use scanner class to take input
System.out.print("Enter Nth term : ");
int n = obj.nextInt();

System.out.print(num1+", "+num2);
for (int i = 2; i < n; i++)  // i=2 because first two terms are already printed
{
num3 = num1 + num2;
System.out.print(", "+num3);
num1 = num2;    // logic of fibonacci series
num2 = num3;
}
System.out.println("\nThese are first "+n+" terms of fibonacci series");
}
}``````

Output:

``````Enter Nth term : 11
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
These are first 11 terms of fibonacci serie
``````

## 2. Fibonacci series using while loop

Same as the above example, Take the input for printing terms up to N. Use N as exit condition in while loop.

### Example

``````import java.util.Scanner;
public class Fibonacci_series {
public static void main(String[] args) {

int a=0, b=1, i=0; // first term of fibonacci series is 0 and second is 1
int c;

Scanner obj = new Scanner(System.in); // use scanner class to take input
System.out.print("Enter Nth term : ");
int n = obj.nextInt();
while (i <n)
{
System.out.print(a + ", ");
c = a + b;
a = b; // logic of fibonacci series
b = c;
i = i + 1;
}
System.out.println("\nThese are first "+n+" terms of fibonacci series");
}
}``````

Output

``````Enter Nth term : 11
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
These are first 11 terms of fibonacci series
``````

## 3. Fibonacci series using dynamic memory allocation

For using dynamic memory allocation in the Fibonacci series java program, Declare a dynamic array of length n+2. Use the same logic of the Fibonacci number and print the array.

### Example

``````public class Fibonacci_series {

public static void main(String[] args)
{
Scanner obj = new Scanner(System.in); // to accept input from user

System.out.print("Enter Nth term : ");
int n = obj.nextInt();

int fib [] = new int[n+2]; //declare a dynamic array
fib[0] = 0; //initialize zero th index with 0
fib[1] = 1; //initialize first index with 1

System.out.print(fib[0]+", "+fib[1]);
for (int i = 2; i < n; i++)
{
fib[i] = fib[i-1] + fib[i-2];
System.out.print(", "+fib[i]);
}
System.out.println("\nThese are first "+n+" terms of fibonacci series");
}
}``````

Output:

``````Enter Nth term : 11
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
These are first 11 terms of fibonacci series
``````

## 4. Fibonacci series using recursion

To use recursion for printing Fibonacci series, take N as input for printing terms of Fibonacci series up to N. Write a static function and give a recursive call to it.

### Example:

``````import java.util.Scanner;

public class Fibonacci_series {

static int num1=0, num2=1; // first term of fibonacci series is 0 and second is 1
static int num3;

static void fibonacci(int n)
{
if(n>0)
{
num3 = num1 + num2;
System.out.print(", "+num3);
num1 = num2; //logic of fibonacci series
num2 =num3;
fibonacci(n-1); //Recursive call
}
}

public static void main(String[] args)
{
Scanner obj = new Scanner(System.in); // use scanner class to take input
System.out.print("Enter Nth term : ");
int n = obj.nextInt();

System.out.print(+num1+", "+num2);
fibonacci(n-2); // n-2 because first two terms are already printed
System.out.println("\nThese are first "+n+" terms of fibonacci series");
}
}``````

Output:

``````Enter Nth term : 11
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
These are first 11 terms of fibonacci series
``````

## How to print the Fibonacci series in java?

``````public class Fibonacci_series {
public static void main(String[] args) {
int n1 =0, n2 =1, n3,n =10;
System.out.print("Fibonacci series is : ");
System.out.print(n1+", "+n2);
for (int i = 2; i < n; i++) {
n3 = n1 + n2;
System.out.print(", "+n3);
n1 = n2;
n2 = n3;
}
}
}
``````
Shares