--- title: Fibonacci Series in Java - Scaler Topics description: Learn about the Program to Print Fibonacci Series in Java by using loops, recursion, & more in this article by Scaler Topics. author: Dipankur Lawari category: Java --- :::section{.main} Fibonacci Series in java is a series of increasing numbers where the first two terms are **0** and **1** respectively. All other subsequent terms are obtained by adding the last two terms before them. Mathematically, **Let f(n) returns us the nth fibonacci number** $$ f(0)=0; f(1)=1; $$ $$ f(n)=f(n-1)+f(n-2); $$ ![Fibonacci Series in Java Introduction](https://www.scaler.com/topics/images/fibonacci-series-in-java-intro.webp) There are several methods to write the Fibonacci series. In this article, we will discuss the following methods in detail: 1. **Fibonacci Series in Java Using for Loop** 1. **Fibonacci Series in Java Using while Loop** 1. **Fibonacci Series in Java Using Iterative Approach** 1. **Fibonacci Series in Java Using recursion** 1. **Fibonacci Series in Java Using Dynamic Programming** ::: :::section{.main} ## Fibonacci Series in Java Using for Loop ```java import java.util.*; public class Example { public static void main(String args[]) { int n, i; Scanner sc = new Scanner(System.in); n = sc.nextInt(); sc.close(); int values[] = new int[n]; // space O(N) used values[0] = 0; values[1] = 1; for (i = 2; i < n; i++) { values[i] = values[i - 1] + values[i - 2]; } System.out.println("The nth fibonacci number is " + values[n - 1]); } } ``` **Output:** ```plaintext 10 The nth fibonacci number is 34 ``` **Explanation:** The Java program calculates the nth number of the Fibonacci sequence, taking n as input from the user. It initializes an integer array of size $n$ to store the Fibonacci sequence and uses a for loop to calculate the remaining elements. Finally, it prints the nth Fibonacci number to the console. **Time Complexity** O(N) - (Linear Time Complexity) This is because, for finding the nth Fibonacci number, we move through an array of size n (actually, we are moving $n-2$ indices in the array). **Space Complexity** O(N) ::: :::section{.main} ## Fibonacci Series in Java Using while Loop ```java import java.util.Scanner; public class Example { public static void main(String args[]) { int n, i; Scanner sc = new Scanner(System.in); n = sc.nextInt(); sc.close(); int values[] = new int[n]; // space O(N) used values[0] = 0; values[1] = 1; i = 2; while (i < n) { values[i] = values[i - 1] + values[i - 2]; i++; } System.out.println("The nth Fibonacci number is " + values[n - 1]); } } ``` **Output:** ```plaintext 10 The nth Fibonacci number is 34 ``` **Explanation:** The Java program calculates the nth number of the Fibonacci sequence using a while loop instead of a for loop. It takes n as input from the user and initializes an integer array of size n to store the Fibonacci sequence. The loop condition `i < n` is used to calculate the remaining elements of the sequence. Finally, it prints the nth Fibonacci number to the console. **Time Complexity** O(N) - (Linear Time Complexity) This is because, to find the nth Fibonacci number, we move through an array of size n (actually, we are moving n-2 indices in the array) using a while loop. **Space Complexity** O(N) ::: :::section{.main} ## Fibonacci Series in Java Using Iterative Approach ```java import java.util.*; class Example { static void generateFibonacci(int N) { int num1 = 0, num2 = 1; for (int i = 0; i < N; i++) { // Print the number System.out.print(num1 + " "); // Swap int num3 = num1 + num2; num1 = num2; num2 = num3; } } // Main method public static void main(String args[]) { // Number of terms in the Fibonacci series int N = 5; // Call the function to generate the Fibonacci series generateFibonacci(N); } } ``` **Output:** ```plaintext 0 1 1 2 3 ``` **Explanation:** The above program generates and prints the Fibonacci series up to 15 terms using the previous two Fibonacci values iteratively. **Time Complexity** O(N) - (Linear Time Complexity) **Space Complexity** O(1) ::: :::section{.main} ## Fibonacci Series in Java Using recursion The function returns the nth Fibonacci number by recursively calling itself for previous values until the input becomes less than or equal to 2. By calling this function to compute each Fibonacci number, we can print the Fibonacci series. ```java import java.util.*; public class Example { public static int fib(int n) { if (n == 0) { return 0; } // Oth fibonacci is 0 if (n == 1 || n == 2) { return 1; } // 1st and 2nd Fibonacci are 1 and 1 only // calling function recursively for nth Fibonacci return fib(n - 1) + fib(n - 2); } public static void main(String args[]) { int n=10; System.out.println("The nth Fibonacci number is " + fib(n - 1)); } } ``` **Output:** ```plaintext The nth Fibonacci number is 34 ``` **Explanation:** In the above example, we defined a recursive function `fib` to get nth Fibonacci number and call it repeatedly (for i = 0 to i = 8) to create a Fibonacci series of length 9. Read to know more about [Fibonacci Series Using recursion in Java](https://www.scaler.com/topics/fibonacci-series-in-java-using-recursion/ ) **Time Complexity** The Time Complexity is $O(2^n)$, i.e. exponential time. **Space Complexity** O(1) ***Join our expert-led [Dynamic Programming Certification](https://www.scaler.com/topics/course/dynamic-programming/) course to master advanced problem-solving techniques and enhance your programming prowess.*** ::: :::section{.main} ## Fibonacci Series in Java Using Dynamic Programming ```java class Example { static int fib(int n) { // Declare an array to store Fibonacci numbers. // 1 extra to handle case, n = 0 int[] fibArray = new int[n + 2]; // Base cases fibArray[0] = 0; fibArray[1] = 1; // Calculate Fibonacci numbers using dynamic programming for (int i = 2; i <= n; i++) { fibArray[i] = fibArray[i - 1] + fibArray[i - 2]; } // Nth Fibonacci Number return fibArray[n]; } public static void main(String[] args) { // Given Number N int N = 5; // Print first N terms of the Fibonacci series for (int i = 0; i < N; i++) { System.out.print(fib(i) + " "); } } } ``` **Output:** ```plaintext 0 1 1 2 3 ``` **Explanation:** In the above program, we use the tabulation method of dynamic programming to calculate the Fibonacci series. It stores the value of the Fibonacci term in the array and calculates further values using the previous two values. **Time Complexity** O(N) **Space Complexity** O(N) ::: :::section{.summary} ## Conclusion The Fibonacci Series in java is a sequence of numbers in which each number (except the first two) is the sum of the two preceding numbers. - The recursive approach uses a function that calls itself to calculate the Fibonacci numbers. It typically has a base case for the first two numbers and recursively computes subsequent numbers. - The time complexity of the recursive approach is exponential, specifically &O(2^n)&, due to repeated function calls and redundant calculations. - The iterative approach has a linear time complexity of `O(n)`, where n is the desired number in the Fibonacci Series. :::