Fibonacci Series in Java without using Recursion
Learn how to generate the Fibonacci series in Java without using recursion.
Implementation in 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
//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
//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
//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
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
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.