# Fibonacci Series in Java

## Introduction

This blog will help you understand the Fibonacci series in Java. The **Fibonacci series in Java** is a program that when given an integer input N, returns a Fibonacci Series of N numbers. Before coding, it is essential to understand what a Fibonacci Series is and what logic is required to solve the problem.

## What is a Fibonacci series in Java?

A Fibonacci series in Java is a sequence of numbers such that every third number is equal to the sum of the previous two numbers. For Example:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Here next number is found by adding up the two numbers before it.

The number 2 is found by adding the two numbers before it hence (1+1),

Similarly, 3 is found by adding the two numbers before it (1+2),

And 5 is (2+3),

For example, the following number in the sequence above is 21+34 = 55

## Understanding the Problem Statement

For a given input integer N, print the Fibonacci Series up to the N term.

```
Example for Fibonacci series in Java:
Input: N = 9
Output: 0 1 1 2 3 5 8 13 21
```

Here we have considered the first term of Fibonacci as 0 and the second as 1 therefore, the term becomes the sum of the first and second numbers, which is 1.

```
Example for Fibonacci series in Java:
Input: N = 14
Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
```

## Ways to Display Fibonacci Series in Java

There are two ways to write the Fibonacci series program in Java:

- Finding Fibonacci series in Java without recursion
- Finding Fibonacci series in Java with recursion

### Fibonacci Series in Java without using recursion

```
//Iterative Fibonacci series program in Java
class Fibonacci{
public static void main(String args[])
{
//Considering the first two numbers as 0 and 1
int num1=0,num2=1,num3,i,count=10;
//Count=10 means that only the first 10 fibonacci numbers will be displayed
System.out.print(num1+" "+num2);
//printing 0 and 1
for(i=2;i<count;++i)
//looping is initiated from 2 as 0 and 1 are already printed
{
num3=num1+num2;
System.out.print(" "+num3);
num1=num2;
num2=num3;
}
}}
```

```
Analysis for Fibonacci series in Java program :
Time Complexity: O(N)
Auxiliary Space: O(1)
```

### Fibonacci Series in Java using recursion

```
//Recursive Fibonacci series program in Java
class FibonacciRec{
static int num1=0,num2=1,num3=0;
static void printFibonacci(int count){
if(count>0){
num3 = num1 + num2;
num1 = num2;
num2 = num3;
System.out.print(" "+num3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(num1+" "+num2);
//printing 0 and 1
printFibonacci(count-2);
//n-2 as 2 numbers are already printed
}
}
```

```
Analysis for Fibonacci series in Java program :
Time Complexity: O(2N)
Auxiliary Space: O(1)
```

You can easily run this code yourself with java online compiler.

## Frequently Asked Questions

### What are Fibonacci series in Java?

A Fibonacci Sequence is a series of whole numbers such that every third number is equal to the sum of the previous two numbers.

### Why is Fibonacci series used in programming?

The Fibonacci numbers provide a model for designing recursive programming algorithms in computer programming, where the time for any routine is the time within the routine itself, as well as the time for the recursive calls.

### What is the use of the Fibonacci series?

The number of petals in a flower follows the Fibonacci sequence. The Fibonacci sequence can also be applied to finance by using four techniques including retracements, arcs, fans, and time zones.

## Conclusion

In this article, we have extensively discussed the Fibonacci series in java and how it can be implemented using multiple techniques. With Java you can also explore C++ and Python - based solutions below.

We hope that this blog helped you to enhance your knowledge regarding Java and if you would like to learn more, check out more articles.