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

// Use While Loop to print Fibonacci series in Java package AdvancedSeries; import java.util.Scanner; public class FibonacciSeries { private static Scanner sc; public static void main(String[] args) { int Number, i = 0, First_Value = 0, Second_Value = 1, Next; sc = new Scanner(System.in); System.out.println("Please Enter any integer Value: "); Number = sc.nextInt(); while(i < Number) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } System.out.println(Next); i++; } } }

**OUTPUT**

**ANALYSIS**

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.

System.out.println("Please Enter any integer Value: "); Number = sc.nextInt();

Below While loop will make sure that, the loop will start from 0 and it is less than the user specified number. Within the While loop, we used If Else statement in Java programming.

- If i value is less than or equal to 1, i value will be assigned to Next.
- If i value is greater than 1, perform calculations inside the Else block.

while(i < Number) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } System.out.println(Next); i++; }

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
is TRUE so Next = 0 and compiler will exit from if statement block**if (0 <= 1)** - Print statement
**System.out.println(Next)**will print the value 0 - Lastly, i will be incremented to 1. Please refer Increment and Decrement Operators in Java article to understand the ++ notation.

**Second Iteration**

- While (1 < 5) is TRUE so, program will start executing statements inside the while loop
- Within the while loop, the If condition
is TRUE so Next = 1 and compiler will exit from if statement block**if (1 <= 1)** - 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
is FALSE so statements inside the else block will start executing**if (2 <= 1)**- 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
is FALSE so statements inside the else block will start executing**if (3 <= 1)**- 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
is FALSE so statements inside the else block will start executing**if (4 <= 1)**- 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

// Program to Print Fibonacci series in Java using For Loop package AdvancedSeries; import java.util.Scanner; public class FibonacciSeriesUsingFor { private static Scanner sc; public static void main(String[] args) { int Number, i, First_Value = 0, Second_Value = 1, Next; sc = new Scanner(System.in); System.out.println("Please Enter any integer Value: "); Number = sc.nextInt(); for(i = 0; i < Number; i++) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } System.out.println(Next); } } }

**OUTPUT**

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

package AdvancedSeries; import java.util.Scanner; public class FibonacciSeriesUsingFunctions { private static Scanner sc; public static void main(String[] args) { int Number; sc = new Scanner(System.in); System.out.println("Please Enter any integer Value: "); Number = sc.nextInt(); Fibonacci_series(Number); } public static void Fibonacci_series(int Number) { int i, First_Value = 0, Second_Value = 1, Next; for(i = 0; i < Number; i++) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } System.out.println(Next); } } }

**OUTPUT**

**ANALYSIS**

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.

package AdvancedSeries; public class Fibonacci { public int Fibonacci_series(int Number) { if(Number == 0) return 0; else if (Number == 1) return 1; else return (Fibonacci_series(Number - 2) + Fibonacci_series(Number - 1)); } }

Within the Main program, we will create an instance of the above specified class and call the methods

package AdvancedSeries; import java.util.Scanner; public class FibonacciSeriesUsingRecusrsion { private static Scanner sc; public static void main(String[] args) { int Number, i; sc = new Scanner(System.in); System.out.println("Please Enter any integer Value: "); Number = sc.nextInt(); Fibonacci fib = new Fibonacci(); for(i = 0; i < Number; i++) { System.out.println(fib.Fibonacci_series(i)); } } }

**OUTPUT**

**ANALYSIS**

**Fibonacci Class Analysis:**

In this java Fibonacci Series program class, we defined a function. Following function will accept integer values as parameter value and return integer value

public int Fibonacci_series(int Number) {

Lets see the Else If statement inside the above specified functions

- if (Number == 0) will check whether the given number is 0 or not. If it is TRUE, function will return the value Zero.
- if (Number == 1) will check whether the given number is 1 or not. If it is TRUE, function will return the value One.
- and, if the number is greater than 1 then the statements inside the else block will be executed.

Within the Else block we called the function recursively to display the Fibonacci series.

return (Fibonacci_series(Number - 2)+ Fibonacci_series(Number - 1));

For example, Number = 2

==> (Fibonacci_series(Number – 2) + Fibonacci_series(Number – 1))

==> (Fibonacci_series(2 – 2) + Fibonacci_series(2 – 1)), It means

==> (Fibonacci_series (0)+ Fibonacci_series(1))

==> return (0 + 1) = return 1

**NOTE:** For Recursive functions it is very important to place a condition before using the function recursively otherwise, we will end up in infinite execution (Same like infinite Loop).

Please be careful :)

**Main Class Analysis:**

Within the Java Fibonacci Series Program main class, first, we created an instance / created an Object of the **Fibonacci** Class

Fibonacci fib = new Fibonacci();

Next, we declare a For Loop and within the for loop we are calling the Fibonacci_series(int Number) method. Here, **System.out.println** statement will print the output.

for(i = 0; i < Number; i++) { System.out.println(fib.Fibonacci_series(i)); }