Java 8 – How to generate Fibonacci numbers using Stream ?

In this article, we will learn how to generate Fibonacci numbers using Stream and other loop structure

Generate Fibonacci Series/Numbers :

  1. Using Java 8 Stream
  2. Using forloop
  3. Using whileloop
  4. Using Recursive function

1. Fibonacci Numbers using Java 8 Stream :

  • Stream.iterate() method generates series of number based on the supplied/passed UnaryOperator
  • Stream.limit() method puts an upper bound on how many numbers need to be generated
  • Stream.map() method convert/transform/extract Arrays of Integers to single Integer
  • Stream.forEach() method prints generated numbers to the console in loop

GenerateFibonacciSeriesUsingJava8.java

package in.bench.resources.arrays.find.number;

import java.util.stream.Stream;

public class GenerateFibonacciSeriesUsingJava8 {

	public static void main(String[] args) {

		System.out.println("Fibonacci Series using Java 8 Stream :- ");

		Stream.iterate(new int[]{0,1}, f -> new int[]{f[1], f[0]+f[1]}) // 1. Iterate fibonacci number
		.limit(15) // 2. limit to 10 Integer numbers
		.map(n -> n[0]) // get 0th index from Arrays of size-2
		.forEach(fibNum -> System.out.print(fibNum + " ")); // 3. print to console
	}
}

Output:

Fibonacci Series using Java 8 Stream :- 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

2. Fibonacci Numbers using for-loop :

  • First, get how many numbers need to be generated for Fibonacci series and declare/define this number as variable limit
  • Then, iterate for-loop from 0 to (limit-1) and inside forloop generate fibonacci series by summing 2 numbers and swapping logic

GenerateFibonacciSeriesUsingForLoop.java

package in.bench.resources.arrays.find.number;

public class GenerateFibonacciSeriesUsingForLoop {

	public static void main(String[] args) {

		System.out.println("Fibonacci Series using for-loop :- ");


		// local variables
		int num1 = 0, num2 = 1;
		int limit = 15;


		// iterate till limit
		for(int i = 0; i < limit; i++) {

			// print Fibonacci series
			System.out.print(num1 + " ");

			// get sum for next Fibonacci number
			int sum = num1 + num2;

			// swap numbers
			num1 = num2;
			num2 = sum;
		}
	}
}

Output:

Fibonacci Series using for-loop :- 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

3. Fibonacci Numbers using while-loop :

  • First, get how many numbers need to be generated for Fibonacci series and declare/define this number as variable limit
  • Then, iterate while-loop from 0 to (limit-1) and inside while-loop generate fibonacci series by summing 2 numbers and swapping logic

GenerateFibonacciSeriesUsingWhileLoop.java

package in.bench.resources.arrays.find.number;

public class GenerateFibonacciSeriesUsingWhileLoop {

	public static void main(String[] args) {

		System.out.println("Fibonacci Series using while-loop :- ");


		// local variables
		int num1 = 0, num2 = 1;
		int limit = 15;
		int iCount = 0;


		// iterate till limit
		while(iCount < limit) {

			// print Fibonacci series
			System.out.print(num1 + " ");

			// get sum for next Fibonacci number
			int sum = num1 + num2;

			// swap numbers
			num1 = num2;
			num2 = sum;

			// increment count
			iCount++;
		}
	}
}

Output:

Fibonacci Series using while-loop :- 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

4. Fibonacci Numbers using Recursive function :

  • First, get how many numbers need to be generated for Fibonacci series and declare/define this number as variable limit
  • Then, invoke a function recursively to generate fibonacci series by summing 2 numbers and swapping logic

GenerateFibonacciSeriesUsingRecursive.java

package in.bench.resources.arrays.find.number;

public class GenerateFibonacciSeriesUsingRecursive {

	// variables
	static int num1 = 0, num2 = 1, sum = 0;



	// main() method
	public static void main(String[] args) {

		// local variables
		int limit = 15;

		System.out.println("Fibonacci Series using Recrusive fucntion :- ");

		// print 1st default numbers
		System.out.print(num1 + " " + num2);

		// call method to print next fibonacci number in recursive fashion
		printFibonacciNumber(limit-2);

	}


	/**
	 * This method prints Fibonacci series
	 * 
	 * @param n
	 */
	private static void printFibonacciNumber(int n) {

		if(n > 0) {

			// get next fibonacci number by summing
			sum = num1 + num2;
			System.out.print(sum + " ");

			// swap numbers
			num1 = num2;
			num2 = sum;

			// call recursive method to print next fibonacci number
			printFibonacciNumber(n-1);
		}
	}
}

Output:

Fibonacci Series using Recrusive fucntion :- 
0 11 2 3 5 8 13 21 34 55 89 144 233 377 

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Roman letter/numbers to Integer ?
Java – How to find/remove first and last digit of a number ?