Fibonacci Series in Java

In this article we will show you, How to Write a Program to Print Fibonacci Series in Java programming language using While Loop, For Loop, Functions and Recursion.

Java Fibonacci Series Program

The Java Fibonacci Series or Fibonacci Numbers are the numbers that are displayed in following sequence:

Fibonacci Series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

If you observe the above pattern, First Value is 0, Second Value is 1 and the subsequent number is the result of sum of the previous two numbers. For example, Third value is (0 + 1), Fourth value is (1 + 1) so on and so forth.

Java Fibonacci Series Program using While Loop

This program for Java Fibonacci Series allows the user to enter any positive integer and then, this program will display the Fibonacci series of number from 0 to user specified number using the While Loop

In this Java Fibonacci Series Program, first, We declared four integer variables i, First_Value and Second_Value and assigned values as we shown above. Following statements will ask the user to enter any positive integer and then, that number is assigned to variable Number.

Let us see the working principle of the while loop in this Java Fibonacci Series program in iteration wise. From the above screenshot you can observe that, User entered value: Number = 5 and as we know i = 0, First_Value = 0, Second_Value = 1

First Iteration

While (0 < 5) is TRUE so, program will start executing statements inside the while loop

Within the while loop we have If Else statement and the condition if (0 <= 1) is TRUE so Next = 0 and compiler will exit from if statement block

While (1 < 5) is TRUE so, program will start executing statements inside the while loop

Within the while loop, the If condition if (1 <= 1) is TRUE so Next = 1 and compiler will exit from if statement block

Print statement System.out.println(Next) will print the value 1

Lastly, i will be incremented to 1

Third Iteration

While (2 < 5) is TRUE so, Java Fibonacci Series program start executing statements inside the while loop.

Within the while loop, the If condition if (2 <= 1) is FALSE so statements inside the else block will start executing

Next = First_Value + Second_Value ==> 0 + 1 = 1

First_Value = Second_Value = 1

Second_Value = Next = 1

Next, Print statement System.out.println(Next) will print the value 1. Lastly, i will be incremented to 1

Fourth Iteration

While (3 < 5) is TRUE so, program will start executing statements inside the while loop.

Within the while loop we have the If condition if (3 <= 1) is FALSE so statements inside the else block will start executing

Next = First_Value + Second_Value ==> 1 + 1 = 2

First_Value = Second_Value = 1

Second_Value = Next = 2

Next, Print statement System.out.println(Next) will print the value 2. Lastly, i will be incremented to 1

Fifth Iteration

While (4 < 5) is TRUE so, program will start executing statements inside the while loop.

Within the while loop we have the If condition if (4 <= 1) is FALSE so statements inside the else block will start executing

Next = First_Value + Second_Value ==> 1 + 2 = 3

First_Value = Second_Value = 2

Second_Value = Next = 3

Next, Print statement System.out.println(Next) will print the value 3. Lastly, i will be incremented to 1

Sixth Iteration

While (5 < 5) is FALSE so, Java Fibonacci Series program will exit from the while loop.

From the above, Our final output of Next values are: 0 1 1 2 3

Java Fibonacci Series Program using For Loop

This Fibonacci Series program in Java allows the user to enter any positive integer and then, this program will display the Fibonacci series of number from 0 to user specified number using Java For Loop

We just replaced the While loop in the above example with the For loop. If you don’t understand the for loop then please refer For Loop article here: Java For Loop

Java Fibonacci Series Program using Functions

This Java Fibonacci Series program using functions allows you to enter any positive integer. And then, this program will display the Fibonacci series of number from 0 to user specified number using Functions

In this java Fibonacci Series program, When the compiler reaches to Fibonacci_series(Number); line in this Java Fibonacci Series Program then the compiler will immediately jump to below function:

public static void Fibonacci_series(int Number) {

We already explained LOGIC above example.

NOTE: If we create a function with Void then there is no need to return any value but, if we declared a function with any data type (int, float etc) then we have return something out from the function.

Java Fibonacci Series Program using Recursion

This program for Java Fibonacci Series allows the user to enter any positive integer and then, this program will display the Fibonacci series of number from 0 to user specified number using Recursion concept.

In this Fibonacci Series Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a method to reverse an integer recursively.