# Java Program to Print Fibonacci Series

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

## Java Fibonacci Series Program using While Loop

This program using a while loop allows entering a positive integer. Then, this Java program displays the Fibonacci series of Numbers from 0 to a given number using the While Loop.

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

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

In this Program, the While loop starts from 0 and iterates up to a given value. Within the While loop, we used the If Else statement in this programming.

First Iteration

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

Second Iteration

• While (1 < 5) is TRUE.
• if (1 <= 1) is TRUE. So, it 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.

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 of the Java Fibonacci series program

• 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 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 program uses For Loop to display the Fibonacci series of numbers from 0 to a number. The code below is the same as the above.

```import java.util.Scanner;

public class FSUsingFor {
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);
}
}
}```

## Java Program for Fibonacci Series of Numbers using Functions

This example uses functions to display the numbers from 0 to a number.

```import java.util.Scanner;

public class FSUsingFunctions {

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();

Fibsr(Number);
}

public static void Fibsr(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);
}
}

}```

In this program, When the compiler reaches the Fibsr(Number) line, it jumps to the below function:

`public static void Fibsr(int Number) {`

## Java Program to Print Fibonacci Series of Numbers using Recursion

This program for the Fibonacci Series displays the numbers from 0 to N using the Recursion and OOPS. In this example, we will first create a class with a method to reverse an integer recursively.

```public class CalFib {
public int fns(int Number) {
if(Number == 0)
return 0;
else if (Number == 1)
return 1;
else
return (fns(Number - 2) + fns(Number - 1));
}

}```

Within the Main, we will create an instance of the class and call the methods.

```import java.util.Scanner;

public class FSUsingRecusrsion {
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();

CalFib fib = new CalFib();
for(i = 0; i < Number; i++)  {
System.out.println(fib.fns(i));
}
}
}```

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

`public int fns(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 TRUE, the function will return the value One.
• And if the number is greater than 1, the statements inside the else block are executed.

We called the function recursively within the Else block to display the result.

`return (fns(Number - 2)+ fns(Number - 1));`

For example, Number = 2

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

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

(fns (0)+ fns(1))

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

Main Class Analysis

Within the Program main class, first, we created an instance / created an Object of the Class.

`CalFib fib = new CalFib();`

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

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