In this post we’ll see a Java program to display Fibonacci series.

Fibonacci series is a series of natural number where next number is the sum of previous two numbers i.e. fn = fn-1 + fn-2.

**For example** – 0, 1, 1, 2, 3, 5, 8, 13, 21 ……

Java program for displaying Fibonacci series can be written using-

- Recursive logic
- Non-recursive logic

#### Fibonacci series using recursion logic

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //user input System.out.println("Enter length of the Fibonacci series: "); int num = sc.nextInt(); for(int i = 0; i < num; i++){ System.out.print(displayFibonacci(i) + " "); } } private static int displayFibonacci(int num){ if(num == 0){ return 0; } if(num == 1){ return 1; } // sum of previous two numbers - calling recursively return displayFibonacci(num - 1) + displayFibonacci(num - 2); } } |

__Output__

1 2 3 |
Enter length of the Fibonacci series: 10 0 1 1 2 3 5 8 13 21 34 |

#### Fibonacci series Java program using iteration

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //user input System.out.println("Enter length of the Fibonacci series: "); int num = sc.nextInt(); displayFibonacci(num); } private static void displayFibonacci(int num){ int num1 = 0; int num2 = 1; int num3 = 0; for(int i = 0; i <num; i++){ System.out.print(num3+" "); num1 = num2; num2 = num3; num3 = num1 + num2; } } } |

__Output__

1 2 3 |
Enter length of the Fibonacci series: 15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |

That’s all for the topic **Fibonacci Series Java Program**. If something is missing or you have something to share about the topic please write a comment.

**You may also like**