Hey Guys in this post we will write program for Fibonacci series using iterative and recursive approach.

**Fibonacci series using iterative approach:**

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 28 29 30 31 32 33 34 35 36 37 38 |
package com.kkjavatutorials.client; import java.util.Scanner; public class FibonacciSeriesUsingIteration { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in);) { System.out.println("Enter How many Fibonacci Series you want::"); int count = scanner.nextInt(); System.out.println("Fibonacci Series::"); printFibonacciSeries(count); } catch (Exception e) { e.printStackTrace(); } } private static void printFibonacciSeries(int count) { int num1=0,num2=1,num3; //printing first two Fibonacci Series numbers 0 and 1 if(count == 1) System.out.print(num1); else if(count >=2) System.out.print(num1+" "+num2); else if(count <=0) throw new RuntimeException("Invalid Count.."); //loop starts from 2 because 0 and 1 are already printed for(int i=2;i<count;++i) { num3=num1+num2; System.out.print(" "+num3); num1=num2; num2=num3; } } } |

**Sample input & output:**

Enter How many Fibonacci Series you want::

10

Fibonacci Series::

0 1 1 2 3 5 8 13 21 34

**Fibonacci series using recursive approach:**

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 28 29 30 31 |
package com.kkjavatutorials.client; import java.util.Scanner; public class FibonacciSeriesUsingRecursion { public static void main(String[] args) { try(Scanner scanner = new Scanner(System.in);) { System.out.println("Enter How many Fibonacci Series you want::"); int num = scanner.nextInt(); System.out.println("Fibonacci Series::"); for (int i = 0; i <=num; i++) { int fs = printFibonacciSeries(i); System.out.println(fs); } } catch (Exception e) { e.printStackTrace(); } } private static int printFibonacciSeries(int n) { // Base Condition if (n <= 1) return n; else { // Recursive case return printFibonacciSeries(n - 1) + printFibonacciSeries(n - 2); } } } |

**Sample input & output:**

Enter How many Fibonacci Series you want::

7

Fibonacci Series::

0 1 1 2 3 5 8 13

That’s all about this topic **Fibonacci series using iterative and recursive approach java program **If you have any doubts or any suggestions to make please drop a comment.