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

**You May Also Like:**

How to swap two numbers with or without temporary variable in java

Java Program to Swap two numbers using Bitwise XOR Operator?

How to reverse a number in Java

How to check Armstrong number java program

Java program to find factorial of a number

Java Program to Calculate the Power of a Number

Check whether a number is prime or not

Java Program to displaying prime numbers

Find largest and second largest number in the given Array

Java program to Remove Duplicate Elements From an Array

Find common elements between two Arrays

Find largest and smallest number in the given Array

Java Program to find duplicate elements in an Array

Count number of words in a string in java

How would you check if a number is even or odd using bit wise operator in Java?

How can you check if the given number is power of 2?

How to check if String is number in java ?

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.