Fibonacci Series in Java

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

Java Fibonacci Series Program using While Loop

This Java Fibonacci series program using a while loop allows entering a positive integer. Then this program displays the Fibonacci series 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++;
		}
	}
}
Program to Print Fibonacci Series in Java 1

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.

Analysis

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 the 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 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.

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.

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);
 }
 }
}
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 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);
 }
 }
 
}
Please Enter any integer Value: 
8
0
1
1
2
3
5
8
13

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

public static void Fibsr(int Number) {

Fibonacci Series in Java using Recursion

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

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));
		}
	}
}
Please Enter any integer Value: 
10
0
1
1
2
3
5
8
13
21
34

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 it is 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 are calling the fns(int Number) method. Here, System.out.println statement will print the output.

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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.