The Fibonacci Number in Java is a fascinating concept where each number in the series is the sum of the two preceding ones. Whether you are a beginner or an experienced programmer, understanding how to generate this sequence is essential. In this article, we will explore various methods to print Fibonacci number in Java.Â
From simple iterative approaches to more advanced recursive techniques, we’ll cover each method in detail, ensuring you gain a clear and comprehensive understanding of the topic. By the end of this guide, you’ll have the skills to implement Fibonacci sequence generation in Java confidently. Let’s explore these methods together and enhance your programming knowledge!
What Is Fibonacci Number In Java
A Fibonacci Number in Java refers to the implementation of the Fibonacci sequence in the Java programming language. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, genrally starting with 0 and 1. In Java, you can generate this sequence using various methods such as iteration, recursion, or dynamic programming. Understanding how to implement and print Fibonacci numbers in Java is a basic foundational exercise that helps in learning about loops, conditional statements, and Java syntax.
Example To Print Fibonacci Number In Java |
Input: N = 11
Output: 0 1 1 2 3 5 8 13 21 34 55 |
Explanation: Here the first term of Fibonacci is 0 and the second is 1, so the 3rd term = first term + second term and 4th term = 2nd term + 3rd term. This sequence continues to follow till the value of N, which is 11 here.
Ways To Print Fibonacci Number In Java
There are 4 way to print fibonacci number series in Java programming language, these 4 methods include-Â
- Printing Fibonacci Series Using Iterative Approach
- Printing Fibonacci Series Using Recursive Approach
- Printing Fibonacci Series Using Memoization
- Printing Fibonacci Series Using Dynamic Programming
Sure, here are the different methods to print the Fibonacci series in Java, along with explanations, code, and sample output:
1. Printing Fibonacci Series Using Iterative Approach
In the iterative approach, we use a loop to calculate the Fibonacci number in Java. We start by initializing the first two numbers in the series as 0 and 1 and then continue to add the last two numbers to get the next number in the series. This process is repeated until we reach the desired count.
Below is the implementation of the iterative approach for printing Fibonacci number in Java
Fibonacci Number In Java Using Iterative Approach |
public class FibonacciIterative {
    public static void main(String[] args) {         int n = 10; // Number of terms to be printed         int num1 = 0, num2 = 1;         System.out.print(“Fibonacci Series using Iterative Approach: “);                for (int i = 1; i <= n; ++i) {             System.out.print(num1 + ” “);             int sum = num1 + num2;             num1 = num2;             num2 = sum;         }     } } |
Output-Â
Fibonacci Series using Iterative Approach: 0 1 1 2 3 5 8 13 21 34Â |
2. Printing Fibonacci Series Using Recursive Approach
In the recursive approach, we define a function that calls itself to calculate the Fibonacci number at a given position. This method is straightforward but can be inefficient for large values due to repeated calculations.
Fibonacci Number In Java Using Recursive Approach |
import java.io.*;
class PWSkills {     // Function to print the fibonacci series     static int fib(int n)     {         // Base Case         if (n <= 1)             return n;         // Recursive call         return fib(n – 1) + fib(n – 2);     }     // Driver Code     public static void main(String args[])     {         // Given Number N         int N = 10;         // Print the first N numbers         for (int i = 0; i < N; i++) {             System.out.print(fib(i) + ” “);         }     } } |
Output-
Fibonacci Series using Recursive Approach: 0 1 1 2 3 5 8 13 21 34Â |
3. Printing Fibonacci Series Using Memoization
Memoization is an optimization technique used to speed up the recursive approach by storing the results of expensive function calls and reusing them when the same inputs occur again. This reduces the time complexity significantly.
Fibonacci Number In Java Using Memoization |
import java.io.*;
class PWSkills {     public static void main(String[] args)     {         // Number of terms to print         int n = 10;         int[] memo = new int[n + 1];         for (int i = 1; i <= n; i++) {             System.out.print(fibonacci(i, memo) + ” “);         }     }     public static int fibonacci(int n, int[] memo)     {         if (memo[n] != 0)             return memo[n];         if (n == 1 || n == 2)             return 1;         else {             memo[n] = fibonacci(n – 1, memo)                       + fibonacci(n – 2, memo);             return memo[n];         }     } } |
Output-Â
Fibonacci Series using Memoization Approach: 0 1 1 2 3 5 8 13 21 34Â |
4. Printing Fibonacci Series Using Dynamic Programming
Dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems. In this approach, we store the Fibonacci numbers in an array and build the sequence from the bottom up.
The basic steps involved in this programming approach are-Â
- Create an array arr[] of size N.
- Initialize arr[0] = 0, arr[1] = 1.
- Iterate over [2, N] and update the array arr[] as- arr[i] = arr[i – 2] + arr[i – 1]
- Print the value of arr[N].
Fibonacci Number In Java Using Dynamic Programming |
import java.io.*;
Class PWSkills{     // Function to find the fibonacci Series     static int fib(int n)     {         // Declare an array to store Fibonacci numbers.         int f[] = new int[n + 2];         int i;         // 0 and 1st number of the series are 0 and 1         f[0] = 0;         f[1] = 1;         for (i = 2; i <= n; i++) {             // Add the previous 2 numbers in the series and store it             f[i] = f[i – 1] + f[i – 2];         }         // Nth Fibonacci Number         return f[n];     }     public static void main(String args[])     {         // Given Number N         int N = 10;         // Print first 10 term         for (int i = 0; i < N; i++)             System.out.print(fib(i) + ” “);     } } |
Output-
Fibonacci Series using Dynamic Programming: 0 1 1 2 3 5 8 13 21 34Â |
These methods illustrate different ways to print the Fibonacci series in Java, each with its own advantages and use cases. By understanding these approaches, you can choose the best one for your specific needs and optimize your code accordingly.
Learn Java Programming With PW Skills
Enroll in our PW Skills Comprehensive Java with DSA Course which will prepare you from the basics to the advanced level along with the demanding concept of Data structures and algorithms. This PW skills holistic course is specially designed by experts providing key features like- mentorship from industrial experts, Daily practice sessions, regular doubt-clearing sessions, placement assistance, soft skills training sessions, alumni support, and much more.Â
Visit PWSkills.com and Enroll today to get exciting Offers and discounts.
Fibonacci Number In Java FAQs
What is the Fibonacci number series?
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. You can refer above in the article to understand it better with the help of examples.
How can I print the Fibonacci series in Java?
You can print the Fibonacci series in Java using various methods such as iterative approach, recursive approach, memoization, and dynamic programming.
Which method is the most efficient for printing Fibonacci numbers in Java?
The dynamic programming approach is generally the most efficient method because it avoids the redundant calculations of the recursive approach and is faster than iterative for large sequences.