# Fibonacci Series in Java

Write a Program to Print Fibonacci Series in Java programming language using While Loop, For Loop, Functions, and Recursion. The Fibonacci Numbers or Fibonacci Series in Java are the numbers that displayed in the following sequence. Fibonacci Series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

## Java Fibonacci Series Program using While Loop

This Java Fibonacci series program allows entering a positive integer. Then this program display the Fibonacci series of numbers from 0 to a given number using the Java While Loop.

```// Use While Loop to print Fibonacci series in Java

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++;
}
}
}```

Java Fibonacci using a while loop output

Let us see the working principle of the while loop in this Java Fibonacci Series program in iteration wise. Number = 5 and as we know i = 0, First_Value = 0, Second_Value = 1

In this Fibonacci Series Program, the While loop starts from 0, and iterate up to given number. Within the While loop, we used If Else statement in Java programming.

Fibonacci First Iteration

• While (0 < 5) is TRUE. So, the program will start executing statements inside the while loop
• Within the while loop, the condition if (0 <= 1) is TRUE. So, Next = 0 and the compiler will exit from if statement block.
• Print statement System.out.println(Next) will print the value 0
• Lastly, i incremented to 1. Please refer to Increment and Decrement Operators in Java article.

Second Iteration

• While (1 < 5) is TRUE.
• if (1 <= 1) is TRUE.
• prints 1
• i will increment to 1

Java Fibonacci Series program Third Iteration

• While (2 < 5) is TRUE.
• if (2 <= 1) is FALSE. So, the 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, it prints 1 and i increments by 1.

Fibonacci Fourth Iteration

• While (3 < 5) is TRUE.
• if (3 <= 1) is FALSE.
• Next = 1 + 1 = 2
• First_Value = Second_Value = 1
• Second_Value = Next = 2
• prints 2.
• i will increment to 1.

Fifth Iteration

• While (4 < 5) is TRUE.
• if (4 <= 1) is FALSE.
• Next = 1 + 2 = 3
• First_Value = Second_Value = 2
• Second_Value = Next = 3
• Next, it prints 3 and increments i by 1.

Sixth Iteration

While (5 < 5) is FALSE. So, the Java Fibonacci program will exit from the while loop. Our final output of the Next values is 0 1 1 2 3

## Java Fibonacci Series Program using For Loop

This Java program uses For Loop to display the Fibonacci series of numbers from 0 to a number.

```// Program to Print Fibonacci series in Java using For Loop

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);
}
}
}```
``````Please Enter any integer Value:
15
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377``````

## Java Fibonacci Series Program using Functions

This program uses functions to display the Fibonacci series of numbers from 0 to a number.

```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);
}
}

}```
``````Please Enter any integer Value:
8
0
1
1
2
3
5
8
13``````

In this Fibonacci Series program, When the compiler reaches to Fibonacci_series(Number); line, then it jumps to below function:

`public static void Fibonacci_series(int Number) {`

## Java Fibonacci Series Program using Recursion

This program for Java Fibonacci Series displays the Fibonacci series of numbers from 0 to N using the Recursion and OOPS. In this Fibonacci Series program, first, we will create a class that 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 Fibonacci 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));
}
}
}```
``````Please Enter any integer Value:
10
0
1
1
2
3
5
8
13
21
34``````

Fibonacci Class Analysis:

In this Fibonacci Series program class, we defined a function. It accepts integer values and return an integer value.

`public int Fibonacci_series(int Number) {`

Let us see the Else If statement inside the above-specified functions

• if (Number == 0) if it is TRUE, the function returns the value Zero.
• if (Number == 1) – if it is TRUE, the function will return the value One.
• And if the number is greater than 1, the statements inside the else block 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.

Main Class Analysis:

Within the 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));
}```