## Introduction

**Fibonacci series** in java is the series of numbers where each next number is the sum of previous two numbers. The first two numbers of Fibonacci series are fixed. Those numbers are 0, 1.

Example of Fibonacci series : **0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55** and so on.

As you can see, first two numbers are 0 and 1 respectively and the next number is the addition of its previous two numbers.

**We can write program for fibonacci series by following ways :**

- Fibonacci Series in using recursion.

- Fibonacci Series without using recursion.
- Using for loop.
- Using while loop.

## Algorithm : Print Fibonacci Series

- Step 1. Assign
**First**element as 0. [First = 0]

- Step 2. Assign
**Second**element as 1. [Second= 1]

- Step 3. Add the First and Second element and assign
**Next**element with the result. [Next = First + Second]

- Step 4. Assign First = Second.

- Step 5. Assign Second = Next.

- Repeat Step 3.

## Program : Fibonacci Series in Java using recursion

Below is the example for printing the fibonacci series using the recursion. Let’s take a look :

### Example 1 : Print Fibonacci Series using recursion

```
public class FibonacciSeries {
static int first = 0, second = 1, num=0;
static void printFibonacciSeries(int cnt){
if(cnt > 0){
num = first + second;
first = second;
second = num;
System.out.print(" "+num);
printFibonacciSeries(cnt-1);
}
}
public static void main(String[] args) {
int seq = 10;
System.out.print("Fibonacci Series of "+seq+" numbers : ");
System.out.print(first+" "+second);
printFibonacciSeries(seq-2);
}
}
```

**Output :**

Fibonacci Series of 10 numbers : 0 1 1 2 3 5 8 13 21 34

## Program : Fibonacci Series without using recursion

There are two ways to print the fibonacci series in Java **without using recursion**, As mentioned below :

- Using
**while**loop. - Using
**for**loop.

### Example 1 : Print Fibonacci Series using while loop

```
public class FibonacciSeries {
public static void main(String[] args) {
int seq = 10, first = 0, second = 1;
System.out.print("Fibonacci Series of "+seq+" numbers : ");
int i=1;
while(i<=seq)
{
System.out.print(first+" ");
int sum = first + second;
first = second;
second = sum;
i++;
}
}
}
```

**Output :**

Fibonacci Series of 10 numbers : 0 1 1 2 3 5 8 13 21 34

### Example 2 : Print Fibonacci Series in Java using for loop

```
public class FibonacciSeries {
public static void main(String[] args) {
int seq = 10, first = 0, second = 1;
System.out.print("Fibonacci Series of "+seq+" numbers : ");
for(int i = 1; i <= seq; ++i)
{
System.out.print(first+" ");
int sum = first + second;
first = second;
second = sum;
}
}
}
```

**Output :**

Fibonacci Series of 10 numbers : 0 1 1 2 3 5 8 13 21 34

## Program : Fibonacci Sequence based on user input

Now let’s learn how to print fibonacci series based on the input received by user. See below example :

### Example 1 : Program to Print Fibonacci Series based on user input

```
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
int seq , first = 0, second = 1;
System.out.println("Enter number of elements you want in the series :");
Scanner scanner = new Scanner(System.in);
seq = scanner.nextInt();
scanner.close();
System.out.print("Fibonacci Series of "+seq+" numbers : ");
for(int i = 1; i <= seq; ++i)
{
System.out.print(first+" ");
int sum = first + second;
first = second;
second = sum;
}
}
}
```

**Output :**

Enter number of elements you want in the series : 10 Fibonacci Series of 10 numbers : 0 1 1 2 3 5 8 13 21 34

**Further Readings **:

## Conclusion

We have learnt different ways of printing the fibonacci series in Java . This is one of the most asked Java interview Question.

Thanks for Reading!