# fibonacci series in java

## Fibonacci Series in Java using Recursion and Loops Program

### What is fibonacci series in java?

A Fibonacci Series in Java is a series of numbers in which the next number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1. The Fibonacci numbers are significantly used in the computational run-time study of an algorithm to determine the greatest common divisor of two integers.

Example:

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

If Fibonacci series is 0 1 1 2 3 5 8 13 21 34

Then solution would be like

0 + 1 = 1

1 + 1 = 2

1 + 2 = 3

2 + 3 = 5

3 + 5 = 8 and so on…

#### Fibonacci series in java

In this test, interviewer may ask write a program which print a “Fibonacci Series” or interviewer may ask that write a program which get input from user and print “Fibonacci Series” of given input.

Here is just example of “Fibonacci Series” if user give input 10 then given 10 number “Fibonacci Series” will be e.g. 1 1 2 3 5 8 13 … . up to a given number.

Here we will write several programs using to print Fibonacci series, you may follow which you easily understand.

This is one of easiest method to find Fibonacci series using for loop.

Method 1
```/*
* Author: Zameer Ali Mohil
* */
public class FibonacciSeries {
/*
* this is one of easiest method to find Fibonacci series using for/
loop
* */
public static void main(String[] args) {
int num, a = 0,b=0, c =1;
num = 10;
System.out.println("Fibonacci Series of the number is:");

for (int i=0; i<num; i++) {
a = b;
b = c;
c = a+b;
System.out.print(a + " ");
}
}
}

```

Output:

Fibonacci Series of the number is:

0 1 1 2 3 5 8 13 21 34

Fibonacci Series Program in Java using While Loop

Method 2
```/*
* Author: Zameer Ali
* */
//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

Fibonacci Series Based On The User Input

Method 3
```/*
* Author: Zameer Ali
* */
//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;
}
}
}
```

Output:

How many numbers you want in Fibonacci:
12
Fibonacci Series of 12 numbers: 0 1 1 2 3 5 8 13 21 34 55 89

Fibonacci Series Using Recursion in Java

Method 4
```/*
* Author: Zameer Ali
* */
//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(n-2) + fibonacciRecursion(n-1);
}
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`

#### Conclusion:

• Fibonacci Series in Java is a series of numbers starting with 0 and 1 and every next number is the sum previous two numbers.
• Bottom-Up Approach builds the solution by moving from smaller indices to larger indices. It uses the default values of first two fibonacci numbers to compute the Nth fibonacci number.
• Bottom-Up Approach to calculate Nth fibonacci number:
• Using For Loop
• Using While Loop
• Top-Down Approach builds the solution by moving from larger indices to smaller indices. It tries to find the fibonacci number for larger indices due to which it goes to smaller indices until base case is arrived.
• Top-Down Approach to calculate Nth fibonacci number:
• Using Recursion without memoization
• Using Recursion with memorization