Java Program to Print Fibonacci Series
In this Java program, we will learn how to print the Fibonacci series using both a traditional Java approach and Java 8 features.
The Fibonacci series is a sequence in which each number is the sum of the previous two numbers.
For example:
0 1 1 2 3 5 8 13 21 34
1. Normal Java Program
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int first = 0;
int second = 1;
System.out.println("Fibonacci Series:");
for (int i = 1; i <= n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
sc.close();
}
}
Output
Enter the number of terms: 10
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
Explanation
The program starts with two numbers:
int first = 0;
int second = 1;
Inside the for loop, the next Fibonacci number is calculated by adding the previous two numbers:
int next = first + second;
The values are then updated:
first = second;
second = next;
This process continues until the requested number of terms is printed.
2. Java 8 Program to Print Fibonacci Series
Java 8 introduced the Stream API, which can be used to generate a Fibonacci series in a more functional programming style.
import java.util.Scanner;
import java.util.stream.Stream;
public class FibonacciJava8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
System.out.println("Fibonacci Series:");
Stream.iterate(
new long[]{0, 1},
f -> new long[]{f[1], f[0] + f[1]}
)
.limit(n)
.forEach(f -> System.out.print(f[0] + " "));
sc.close();
}
}
Output
Enter the number of terms: 10
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
Explanation of Java 8 Program
The Java 8 version uses Stream.iterate() to generate Fibonacci numbers.
Stream.iterate(
new long[]{0, 1},
f -> new long[]{f[1], f[0] + f[1]}
)
The array stores two consecutive Fibonacci numbers.
For every iteration:
- The first value becomes the second value.
- The second value becomes the sum of the two previous values.
The limit(n) method controls how many Fibonacci terms are generated:
.limit(n)
Finally, forEach() prints each generated value:
.forEach(f -> System.out.print(f[0] + " "));
Here, f -> is a Java 8 lambda expression.
Difference Between Normal Java and Java 8
| Feature | Normal Java | Java 8 |
|---|---|---|
| Approach | for loop | Stream API |
| Java 8 feature | No | Stream.iterate() |
| Lambda expression | No | Yes |
| Easy for beginners | Yes | Moderate |
| Performance | Simple and efficient | More functional style |
Which Approach Should You Use?
For beginners and general-purpose Java programming, the normal for loop approach is recommended because it is easier to understand and maintain.
The Java 8 version is useful for learning how the Stream API and lambda expressions can be applied to sequence generation.
Conclusion
The Fibonacci series is a common Java programming problem used in coding interviews and programming exercises. The traditional approach uses a loop and variables, while the Java 8 approach demonstrates how Streams and Lambda expressions can generate the same sequence.