Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Fibonacci series in Java?
2.1.
Examples of Fibonacci Series in Java
3.
Fibonacci Series in Java without using Recursion
3.1.
Implementation in Java 
3.2.
Java
4.
Fibonacci Series in Java using Recursion
4.1.
Implementation in Java 
4.2.
Java
5.
Fibonacci Series in Java Using Iterative Approach
5.1.
Implementation in Java
5.2.
Java
6.
Fibonacci Series in Java Using Dynamic Programming
6.1.
Implementation in Java
6.2.
Java
7.
Fibonacci Series Using Recursive Approach
7.1.
Java
8.
Fibonacci Series Using Memoization
8.1.
Java
9.
Frequently Asked Questions
9.1.
What is the pattern of 1 1 2 3 5 8 in Java?
9.2.
What is Fibonacci series code? 
9.3.
How to print Fibonacci series in Java using scanner?
10.
Conclusion
Last Updated: Aug 12, 2024
Easy

Fibonacci Series in Java

Author Parth Jain
0 upvote

Introduction

Understanding the Fibonacci Series in Java: A sequence of numbers where each number is the sum of the two preceding ones, commonly initiated with 0 and 1. It is widely used in programming and algorithms for its recursive properties.

Fibonacci Series in Java

This blog will help you understand the Fibonacci series in Java. In this article, we will see the Java Program to display Fibonacci series. Before coding, it is essential to understand what a Fibonacci Series is and what logic is required to solve the problem.

What is a Fibonacci series in Java?

In Java, a Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting with 0 and 1. It is often implemented using recursion or iteration. 

Examples of Fibonacci Series in Java

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Here, the next number is found by adding up the two numbers before it.
The number 2 is found by adding the two numbers before it hence (1+1),
Similarly, 3 is found by adding the two numbers before it (1+2),
And 5 is (2+3),
For example, the following number in the sequence above is 21+34 = 55

There are different ways to write the Fibonacci series program in Java:

  • Finding the Fibonacci series in Java without recursion
  • Finding Fibonacci series in Java with recursion
  • Fibonacci Series in Java Using Iterative Approach
  • Fibonacci Series in Java Using Dynamic Programming

Fibonacci Series in Java without using Recursion

Learn how to generate the Fibonacci series in Java without using recursion.

Implementation in Java 

  • Java

Java

//Fibonacci Series in Java without using recursion


class Fibonacci{ 
public static void main(String args[]) 
{  
//Considering the first two numbers as 0 and 1
int num1=0,num2=1,num3,i,count=10; 
//Count=10 means that only the first 10 fibonacci numbers will be displayed 
System.out.print(num1+" "+num2);
//printing 0 and 1
for(i=2;i<count;++i) 
//looping is initiated from 2 as 0 and 1 are already printed 
{
 num3=num1+num2;
 System.out.print(" "+num3);
 num1=num2;
 num2=num3;
}
}}


Analysis of Fibonacci Series in Java without using recursion:

Time Complexity: O(N) 

Auxiliary Space: O(1)

Fibonacci Series in Java using Recursion

Learn how to generate the Fibonacci series in Java using recursion.

Implementation in Java 

  • Java

Java

//Fibonacci Series in Java using recursion

class FibonacciRec{ 
static int num1=0,num2=1,num3=0;   
static void printFibonacci(int count){   
   if(count>0){   
        num3 = num1 + num2;   
        num1 = num2;   
        num2 = num3;   
        System.out.print(" "+num3);  
        printFibonacci(count-1);   
    }   
}   
public static void main(String args[]){   
int count=10;   
 System.out.print(num1+" "+num2);
//printing 0 and 1   
 printFibonacci(count-2);
//n-2 as 2 numbers are already printed  

You can easily run this code yourself with Java online compiler.

Analysis of the Fibonacci Series in Java using recursion:

Time Complexity: O(2N)  

Auxiliary Space: O(1)

Fibonacci Series in Java Using Iterative Approach

Learn how to generate the Fibonacci Series in Java Using Iterative Approach.

Implementation in Java

  • Java

Java

//Fibonacci Series in Java Using Iterative Approach

public class FibonacciSeries {
public static void main(String[] args) {
int n = 10; // Number of Fibonacci numbers to generate
long[] fibonacci = new long[n];
// Initialize the first two Fibonacci numbers
fibonacci[0] = 0;
fibonacci[1] = 1;
// Generate the Fibonacci series iteratively
for (int i = 2; i < n; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
// Print the Fibonacci series
System.out.println("Fibonacci Series:");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci[i] + " ");
}
}
}

You can easily run this code yourself with Java online compiler.

Analysis of the Fibonacci Series in Java Using Iterative Approach:

Time Complexity: O(N)  

Auxiliary Space: O(N)

Fibonacci Series in Java Using Dynamic Programming

Learn how to generate the Fibonacci Series in Java Using Dynamic Programming.

Implementation in Java

  • Java

Java

//Fibonacci Series in Java Using Dynamic Programming

public class FibonacciDP {
public static void main(String[] args) {
int n = 10; // Change n to the desired number of terms
long[] fib = new long[n];

fib[0] = 0; // First Fibonacci number
fib[1] = 1; // Second Fibonacci number

// Calculate the Fibonacci series using dynamic programming
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}

// Print the Fibonacci series
System.out.println("Fibonacci Series:");
for (int i = 0; i < n; i++) {
System.out.print(fib[i] + " ");
}
}
}

You can easily run this code yourself with Java online compiler.

Analysis of the Fibonacci Series in Java Using Dynamic Programming:

Time Complexity: O(N)  

Auxiliary Space: O(N)

Fibonacci Series Using Recursive Approach

The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. The recursive approach involves defining a base case and a recursive step.

Example:

  • Java

Java

public class FibonacciRecursive {
public static void main(String[] args) {
int n = 10; // Find the first 10 numbers in the Fibonacci series
System.out.print("Fibonacci Series using Recursion: ");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
}

public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}

 

Output:

Fibonacci Series using Recursion: 0 1 1 2 3 5 8 13 21 34

Fibonacci Series Using Memoization

Memoization is an optimization technique used to speed up recursive algorithms by storing the results of expensive function calls and reusing them when the same inputs occur again.

Example:

  • Java

Java

import java.util.HashMap;

public class FibonacciMemoization {
private static HashMap<Integer, Integer> memo = new HashMap<>();

public static void main(String[] args) {
int n = 10; // Find the first 10 numbers in the Fibonacci series
System.out.print("Fibonacci Series using Memoization: ");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
}

public static int fibonacci(int n) {
if (n <= 1) {
return n;
}

// Check if the value is already computed
if (memo.containsKey(n)) {
return memo.get(n);
}

// Compute and store the result
int result = fibonacci(n - 1) + fibonacci(n - 2);
memo.put(n, result);

return result;
}
}

 

Output:

Fibonacci Series using Memoization: 0 1 1 2 3 5 8 13 21 34

Frequently Asked Questions

What is the pattern of 1 1 2 3 5 8 in Java?

The pattern "1 1 2 3 5 8" represents the Fibonacci sequence. In this sequence, each number is the sum of the two preceding ones, starting with 1. The pattern continues indefinitely: 13, 21, 34, and so on.

What is Fibonacci series code? 

A Fibonacci series code in Java generates a sequence where each number is the sum of the two preceding ones. It typically starts with 0 and 1. The series can be implemented using recursion or iteration. For example, the code iteratively calculates and prints the Fibonacci sequence based on user-defined length.

How to print Fibonacci series in Java using scanner?

To print the Fibonacci series in Java using Scanner, import the java.util.Scanner class. Use Scanner to get user input for the desired length of the series. Then, employ an iterative loop to calculate and print the Fibonacci sequence. For example, using Scanner to take user input for the series length and printing the resulting Fibonacci sequence.

Conclusion

In conclusion, implementing the Fibonacci series in Java involves using either recursion or iteration. The series starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. The choice between recursion and iteration depends on efficiency and programming preferences. Scanner can be employed to take user input for the length of the series. Overall, understanding the iterative or recursive logic behind the Fibonacci series is crucial for writing effective Java code.

We hope that this blog helped you to enhance your knowledge regarding the Fibonacci series in Java and if you would like to learn more, check out more articles.

Live masterclass