# Fibonacci Series Program in Java using Loops & Recursion

## What is Fibonacci Series?

In Fibonacci series, next number is the sum of previous two numbers. The first two numbers of Fibonacci series are 0 and 1.

The Fibonacci numbers are significantly used in the computational run-time study of algorithm to determine the greatest common divisor of two integers.In arithmetic, the Wythoff array is an infinite matrix of numbers resulting from the Fibonacci sequence.

`The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...`

### Java code using For Loop

• //Using For Loop
•
• public class FibonacciExample {
•
•
• public static void main(String[] args)
•
• {
•
• // Set it to the number of elements you want in the Fibonacci Series
•
• int maxNumber = 10;
•
• int previousNumber = 0;
•
• int nextNumber = 1;
•
•
• System.out.print(“Fibonacci Series of “+maxNumber+” numbers:”);
•
•
• for (int i = 1; i <= maxNumber; ++i)
• {
•
• System.out.print(previousNumber+” “);
•
• /* On each iteration, we are assigning second number
•
• * to the first number and assigning the sum of last two
•
• * numbers to the second number
•
• */
•
•
•
• int sum = previousNumber + nextNumber;
•
• previousNumber = nextNumber;
•
• nextNumber = sum;
• }
•
• }
•
• }

Output:

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

Program Logic:

• previousNumber is initialized to 0 and nextNumber is initialized to 1
• For Loop iterates through `maxNumber`
• Display the previousNumber
• Calculates sum of previousNumber and nextNumber
• Updates new values of previousNumber and nextNumber

### Java code using While Loop

You can also generate Fibonacci Series using a `While` loop in Java.

• //Using While Loop
•
• public class FibonacciWhileExample {
•
•
• public static void main(String[] args)
• {
•
•
• int maxNumber = 10, previousNumber = 0, nextNumber = 1;
•
•
• System.out.print(“Fibonacci Series of “+maxNumber+” numbers:”);
•
•
• int i=1;
•
•
• while(i <= maxNumber)
• {
•
•
• System.out.print(previousNumber+” “);
•
•
• int sum = previousNumber + nextNumber;
•
•
• previousNumber = nextNumber;
•
•
• nextNumber = sum;
•
• i++;
• }
•
• }
•
• }

Output:

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

The only difference in the program logic is use of WHILE Loop to print Fibonacci Numbers

### Fibonacci Series Based On The User Input

• //fibonacci series based on the user input
•
• import java.util.Scanner;
•
• public class FibonacciExample {
•
•
• public static void main(String[] args)
• {
•
•
• int maxNumber = 0;
•
• int previousNumber = 0;
•
• int nextNumber = 1;
•
• System.out.println(“How many numbers you want in Fibonacci:”);
•
• Scanner scanner = new Scanner(System.in);
•
• maxNumber = scanner.nextInt();
•
• System.out.print(“Fibonacci Series of “+maxNumber+” numbers:”);
•
•
• for (int i = 1; i <= maxNumber; ++i)
• {
•
• System.out.print(previousNumber+” “);
•
• /* On each iteration, we are assigning second number
•
• * to the first number and assigning the sum of last two
•
• * numbers to the second number
•
• */
•
•
•
• int sum = previousNumber + nextNumber;
•
• previousNumber = nextNumber;
•
• nextNumber = sum;
• }
•
• }
•
• }

Program Logic:

The logic is same as earlier. Instead of hardcoding the number of elements to show in Fibonacci Series, the user is asked to write number.

### Java code using Recursion

• //Using Recursion
•
• public class FibonacciCalc{
•
• public static int fibonacciRecursion(int n){
•
• if(n == 0){
•
• return 0;
•
• }
•
• if(n == 1 || n == 2){
•
• return 1;
•
• }
•
• return fibonacciRecursion(n2) + fibonacciRecursion(n1);
•
•
• }
•
• public static void main(String args[]) {
•
• int maxNumber = 10;
•
• System.out.print(“Fibonacci Series of “+maxNumber+” numbers: “);
•
• for(int i = 0; i < maxNumber; i++){
•
• System.out.print(fibonacciRecursion(i) +” “);
•
• }
•
• }
•
• }

Output:

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

Program Logic:

A recursive function is one that has the capability to call itself.

fibonacciRecursion():

1. Takes an input number. Checks for 0, 1, 2 and returns 0, 1, 1 accordingly because Fibonacci sequence starts with 0, 1, 1.
2. When input n is >=3, The function will call itself recursively. The call is done two times. Let’s see example for input of 4.
```	fibonacciRecursion (4)
It will recursively call fibonacciRecursion function for values 2 and 3
fibonacciRecursion (2) \\ call for value 0 and 1
fibonacciRecursion (0) = 0
fibonacciRecursion (1) = 1
fibonacciRecursion (3) \\ It will call for 1 and 2
fibonacciRecursion (1) = 1
fibonacciRecursion (2) \\ It will call for 0 and 1
fibonacciRecursion (0) = 0
fibonacciRecursion (1) = 1
```  