Home »
Java Programs »
Core Java Example Programs
Java program to print Fibonacci Series
Print Fibonacci Series in Java - This program will read number of terms and prints the Fibonacci Series. Fibonacci Series is a series in which term is the sum of previous two terms.
Print Fibonacci Series using Java Program
/*Java program to print Fibonacci Series.*/
import java.util.Scanner;
public class Fabonacci {
public static void main(String[] args) {
int SeriesNum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of fibonacci series : ");
SeriesNum = sc.nextInt();
int[] num = new int[SeriesNum];
num[0] = 0;
num[1] = 1;
//number should be sum of last two numbers of Series
for (int i = 2; i < SeriesNum; i++) {
num[i] = num[i - 1] + num[i - 2];
}
System.out.println("fibonacci series : ");
for (int i = 0; i < SeriesNum; i++) {
System.out.print(num[i] + " ");
}
}
}
Output
Enter the length of fibonacci series : 10
fibonacci series :
0 1 1 2 3 5 8 13 21 34
Core Java Example Programs »