Fibonacci Series in Java
Here, you will get and learn the program code to print the Fibonacci Series in Java programming.
Fibonacci series is a sequence of numbers, In which, each number is the sum of the two last numbers, starting from 0 and 1. The Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.
Print Fibonacci Series in Java using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //Fibonacci Series program in Java import java.util.*; public class FibonacciSeries { public static void main(String args[]) { int num, x1 = 0, x2 = 1; Scanner sc = new Scanner(System.in); System.out.println("Enter terms : "); num = sc.nextInt(); System.out.print("First " + num + " terms: "); for (int i = 1; i <= num; ++i) { System.out.print(x1 + " "); int sum = x1 + x2; x1 = x2; x2 = sum; } } } |
Output
C:\CodeRevise\java>javac FibonacciSeries.java
C:\CodeRevise\java>java FibonacciSeries
Enter terms :
15
First 15 terms: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Check out our other Java Programming Examples