# Java Program to Display Fibonacci Series

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

## Program to Display Fibonacci Series

In this article we will se different approaches to display Fibonacci series. Before going to the actual concept let’s first understand what is this Fibonacci series.

Fibonacci series : It represents a series of numbers where the next number is the sum of previous two numbers.

For example:
Fibonacci series upto 10 terms
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Logic:

 next=one+two;
one=two;
two=next;

Let’s see one by one:

### Method-1 : Fibonacci Series using for loop

By using for loop we can get the Fibonacci series.

Approach:

1. Take the first two terms of Fibonacci series as 0 and 1 and assign them to two integer variables say ‘one‘, ‘two‘ respectively.
2. Then take a third variable say ‘count‘, to represent numbers of terms in Fibonacci series.
3. Then take a for loop to get the series upto mentioned term by using Fibonacci logic.
4. Go on printing one by one series number in the for loop.

See the below program to understand it clearly.

class Fibo
{
public static void main(String[] args)
{

int one = 0, two = 1, count=10;
System.out.println("Fibonacci Series upto " + count + " terms:");

// for loop to get fibonacci till 10 terms
for (int i = 1; i <= count; i++)
{
System.out.print(one + "  ");

// computing the next term
int next = one+two;
one = two;
two = next;
}
}
}
Output:

Fibonacci Series upto 10 terms:
0 1 1 2 3 5 8 13 21 34

### Method-2 : Fibonacci Series using while loop

By using while loop we can get the Fibonacci series.

Approach:

1. Approach is completely same as for loop, only in this we will loop through while instead of for.

See the below program to understand it clearly.

class Fibo
{
public static void main(String[] args)
{

int one = 0, two = 1, count=10;
System.out.println("Fibonacci Series upto " + count + " terms:");

// i value initialized
int i=1;
// while loop to get fibonacci till 10 terms
while(i<=count)
{
System.out.print(one + "  ");

// computing the next term
int next = one+two;
one = two;
two = next;

//incrementing i
i++;
}
}
}
Output:

Fibonacci Series upto 10 terms:
0 1 1 2 3 5 8 13 21 34

### Method-3 : Fibonacci Series using recursion

By using recursion also we can get the Fibonacci series.

Approach:

1. Take a user defined method say Fibonacci() method.
2. Then put Fibonacci logic inside it.
3. Then call that Fibonacci() method repeatedly according to the Fibonacci terms say ‘count‘ value.

See the below program to understand it clearly.

class Fibo
{
// one and two value initialized
static int one=0,two=1,next=0;

// user defined method
static void Fibonacci(int count)
{
// will be executed till count value is greater than 0
// means inside this Fibonacci() method will be called recursively 10 times.
if(count>0)
{
System.out.print(one+" ");
int next = one + two;
one = two;
two = next;
// Fibonacci() method calling itself i.e. recursion
Fibonacci(count-1);
}
}

// main method
public static void main(String args[])
{
// count value initialized
int count=10;
// prinitng the fibonacci series
System.out.println("Fibonacci Series upto " + count + " terms:");
// calling the Fibonacci() method
Fibonacci(count);
}
}

Output:

Fibonacci Series upto 10 terms:
0 1 1 2 3 5 8 13 21 34

Related Basic Java Programs: