Fibonacci Series In Java

Hey guys!! Welcome to flower brackets blog. Today we will learn to develop Fibonacci series in java.

Basically there are some programs which are expected generally from job seekers and some time asked during viva.

Out of which Fibonacci series is most common program.

Before developing Fibonacci series program let’s understand Fibonacci series and logic behind this program.

Fibonacci Series

Fibonacci Series In Java

As you can see above image we have Fibonacci series.

By definition Fibonacci series is an integer sequence where first two numbers are zero, one and each subsequent number is the sum of previous two numbers.

In the above image as you can see I have added each number. Now zero plus one becomes one.

Also Read – Matrix Transpose In Java

That is the third term of Fibonacci series. If we add one plus one we will get the value of two.

Again if we add one and two we will get three. Likewise if you add two and three we will get value five.

In the similar way we will get the other terms of the Fibonacci series.

example: Fibonacci Series In Java

Now let’s see an example of Fibonacci series

public class FibonacciDemo
{
public static void main(String[] args)
{
int fiboNumber = 15;
int fibonacci[] = new int[fiboNumber];
fibonacci[0] = 0;
fibonacci[1] = 1;

for(int a = 2; a < fiboNumber; a++)
{
fibonacci[a] = fibonacci[a - 1] + fibonacci[a - 2];
}

System.out.println("Fibonacci Series - ");
for(int a = 0; a < fiboNumber; a++)
{
System.out.println(fibonacci[a] + " ");
}
}
}

Output:

Fibonacci series –
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377


other ways

You can execute Fibonacci series program in java using while loop.

Here’s the example

public class FibonacciExample1
{
public static void main(String args[])
{
int x = 0;
int y = 1;
int z;

System.out.println("Fibonacci Series - "); System.out.println(x);
System.out.println(y);
z = x + y;
System.out.println(z);

while(z < 144)
{
x = y;
y = z;
z = x + y;
System.out.println(z);
}
}
}

Output:

Fibonacci Series –
0
1
1
2
3
5
8
13
21
34
55
89
144


conclusion:

So that’s it about Fibonacci series in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

If you like this post share it across social networks.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *