Java – Fibonacci Series
Fibonacci series is a series of numbers in which at any point an element is equal to the sum of its previous immediate two terms. The first two terms are zero and 1.
In this tutorial, we learn to write Java programs that print Fibonacci series using different looping techniques.
Fibonacci Series using For Loop
In this example, we shall use Java For Loop to generate an array with given number of terms of Fibonacci series.
Java Program
/**
 * Java Program - Fibonacci
 * 0 1 1 2 3 5 8 13 21 34 55 . .
 */
public class Fibonacci {
	public static void main(String[] args) {
		int nterms = 10;
		
		int[] fibo = new int[nterms];
		
		for (int index=0; index < fibo.length; index++) {
			if(index==0) {
				fibo[index] = 0;
			} else if(index==1) {
				fibo[index] = 1;
			} else {
				fibo[index] = fibo[index-1] + fibo[index-2]; 
			}
		}
		
		//print fibonacci series
		for(int n:fibo)
			System.out.print(n+"  ");
		
	}
}
Output
0  1  1  2  3  5  8  13  21  34
Algorithm to print Fibonacci series of n terms
We shall use the following algorithm to print Fibonacci series of n terms. You can use while loop or for loop to realize this algorithm.
- Start.
 - Get the value of n in a variable 
nTerms. We shall generate first n terms in Fibonacci series. - Take two variables n1 and n2. Initialize these with 0 and 1 respectively.
 - Initialize i with 1.
 - Check if i is less than or equal to 
nTerms. If false go to step 9. - Print n1.
 - Compute sum of n1 and n2, and load n1 with n2, and n2 with the sum.
 - Increment i, and go to step 5.
 - Stop.
 
Fibonacci Series using For Loop and Without Array
In this example, we shall use Java For Loop to print given number of terms in a Fibonacci series.
Java Program
/**
 * Java Program - Fibonacci
 * 0 1 1 2 3 5 8 13 21 34 55 . .
 */
public class Fibonacci {
	public static void main(String[] args) {
		int nTerms = 10; //number of elements in fibonacci sequence
		int n1 = 0, n2 = 1; //previous two elements
		for (int i=1; i <= nTerms; i++) {
			System.out.print(n1 + "  ");
			int n = n1 + n2;
			n1 = n2;
			n2 = n;
		}
	}
}
Output
0  1  1  2  3  5  8  13  21  34
Fibonacci Series using While Loop
In this example, we shall use Java While Loop to print n terms of a Fibonacci series.
Java Program
/**
 * Java Program - Fibonacci
 * 0 1 1 2 3 5 8 13 21 34 55 . .
 */
public class Fibonacci {
	public static void main(String[] args) {
		int nTerms = 10; //number of elements in fibonacci sequence
		int n1 = 0, n2 = 1; //previous two elements
		int i=1;
		while (i <= nTerms) {
			System.out.print(n1 + "  ");
			int n = n1 + n2;
			n1 = n2;
			n2 = n;
			i++;
		}
	}
}
Output
0  1  1  2  3  5  8  13  21  34
Algorithm to print Fibonacci series with last term less than Given Number
We shall use the following algorithm to print Fibonacci series with last term less than a given maximum limit. You can use while loop or for loop to realize this algorithm.
- Start.
 - Get the value of maximum limit of Fibonacci term in a variable 
max. - Take two variables 
n1andn2. Initialize these with 0 and 1 respectively. - Initialize 
iwith1. - Check if 
n1is less thanmax. If false go to step 9. - Print 
n1. - Compute sum of 
n1andn2, and loadn1withn2, andn2with the sum. - Increment 
i, and go to step 5. - Stop.
 
Fibonacci Series with Last Term less than Given Number
In this example, we shall print all the terms of a Fibonacci series whose last term is less than a given maximum limit.
Java Program
/**
 * Java Program - Fibonacci
 * 0 1 1 2 3 5 8 13 21 34 55 . .
 */
public class Fibonacci {
	public static void main(String[] args) {
		int max = 100; //number of elements in fibonacci sequence
		int n1 = 0, n2 = 1; //previous two elements
		for (int i=1; n1 < max; i++) {
			System.out.print(n1 + "  ");
			int n = n1 + n2;
			n1 = n2;
			n2 = n;
		}
	}
}
Output
0  1  1  2  3  5  8  13  21  34  55  89
Fibonacci Series using For Loop and Reduced Lines of Code
From the previous example, we shall rearrange the code such that, we have fewer lines of code in and around for loop.
Java Program
/**
 * Java Program - Fibonacci
 * 0 1 1 2 3 5 8 13 21 34 55 . .
 */
public class Fibonacci {
	public static void main(String[] args) {
		int max = 100; //max term in Fibonacci sequence
		for (int n1 = 0, n2 = 1, n; n1 <= max; n=n1+n2, n1=n2, n2=n)
			System.out.print(n1 + "  ");
		
	}
}
Output
0  1  1  2  3  5  8  13  21  34  55  89
Conclusion
In this Java Tutorial, we learned how to write Java Programs using looping statements to generate Fibonacci Series.
