The Fibonacci series is a series of elements where, the previous two elements are added to get the next element, starting with 0 and 1. In this article, we will learn how to print Fibonacci Series in Java up to the N term where N is the given number.
Examples of Fibonacci Series in Java
Input: N = 10
Output: 0 1 1 2 3 5 8 13 21 34
Here first term of Fibonacci is 0 and second is 1, so that 3rd term = first(o) + second(1) etc and so on.
Methods to Print Fibonacci Series in Java
There are many methods to print Fibonacci Series in Java as mentioned below:
- Iterative Approach
- Recursive Approach
- Using Memoization
- Using D.P.(Dynamic Programming)
1. Iterative Approach to Print Fibonacci Series in Java
Initialize the first and second numbers to 0 and 1. Following this, we print the first and second numbers. Then we send the flow to the iterative while loop where we get the next number by adding the previous two numbers and simultaneously we swap the first number with the second and the second with the third.
Below is the implementation of the above approach:
Java
class GFG {
static void Fibonacci( int N)
{
int num1 = 0 , num2 = 1 ;
int counter = 0 ;
while (counter < N) {
System.out.print(num1 + " " );
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1 ;
}
}
public static void main(String args[])
{
int N = 10 ;
Fibonacci(N);
}
}
|
Output
0 1 1 2 3 5 8 13 21 34
The complexity of the above method
Time Complexity: O(N)
Auxiliary Space: O(1)
2. Fibonacci Series Using Recursion in Java
Since Fibonacci Number is the summation of the two previous numbers. We can use recursion as per the following condition:
- Get the number whose Fibonacci series needs to be calculated.
- Recursively iterate from value N to 1:
- Base case: If the value called recursively is less than 1, the return 1 the function.
- Recursive call: If the base case is not met, then recursively call for the previous two values as:
recursive_function(N – 1) + recursive_function(N – 2);
- Return statement: At each recursive call(except the base case), return the recursive function for the previous two values as:
recursive_function(N – 1) + recursive_function(N – 2);
Below is the implementation of the above approach:
Java
class GFG {
static int fib( int n)
{
if (n <= 1 )
return n;
return fib(n - 1 ) + fib(n - 2 );
}
public static void main(String args[])
{
int N = 10 ;
for ( int i = 0 ; i < N; i++) {
System.out.print(fib(i) + " " );
}
}
}
|
Output
0 1 1 2 3 5 8 13 21 34
The complexity of the above method
Time Complexity: O(2N)
Auxiliary Space: O(1)
3. Fibonacci Series Using Memoization
In the above example, Its time complexity is O(2n) which can reduce to O(n) using the memoization technique which will help to optimize the recursion method. This is because the function computes each Fibonacci number only once and stores it in the array.
Below is the implementation of the above approach:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
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
1 1 2 3 5 8 13 21 34 55
The Complexity of the above method
Time Complexity: O(n)
Auxiliary Space: O(n)
4. Printing Fibonacci Series Using Dynamic Programming
We can avoid the repeated work done in method 2 by storing the Fibonacci numbers calculated so far.
The steps to writing the Program are mentioned below:
- 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].
Below is the implementation of the above approach:
Java
class fibonacci {
static int fib( int n)
{
int f[] = new int [n + 2 ];
int i;
f[ 0 ] = 0 ;
f[ 1 ] = 1 ;
for (i = 2 ; i <= n; i++) {
f[i] = f[i - 1 ] + f[i - 2 ];
}
return f[n];
}
public static void main(String args[])
{
int N = 10 ;
for ( int i = 0 ; i < N; i++)
System.out.print(fib(i) + " " );
}
}
|
Output
0 1 1 2 3 5 8 13 21 34
The complexity of the above method
Time Complexity: O(N)
Auxiliary Space: O(N)
To know more about the Fibonacci Series, refer to the article – Program to print first n Fibonacci Numbers.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Jul, 2023
Like Article
Save Article