Write a program in Java to print Fibonacci Series

In this article we are going to discuss a problem from arrays; this is one of the most commonly asked interview question of today.
 

Write a Program in Java to print Fibonacci Series, this can be done using an array.

e.g. if {0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 }

Here is a straight forward solution for the problem, We can print Fibonacci Series using some mathematical calculations on an array as shown in example below:

package com.tbNext.byexample;

public class TBPrintFibonacciSeries {

	public static void main(String[] args) {
		int elementsToPrint = 15;
		int[] intArray = new int[elementsToPrint];
		intArray[0] = 0;
		intArray[1] = 1;

		for (int i = 2; i < intArray.length; i++) {
			intArray[i] = intArray[i - 1] + intArray[i - 2];
		}
		System.out.println("Print Fibonacci Series");
		for (int i = 0; i < intArray.length; i++) {
			System.out.print(intArray[i] + " ");
		}
	}
}


Output:

Print Fibonacci Series
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377


That?s it, we have seen Write a Program in Java to print Fibonacci Series, this can be done using an array. In upcoming articles we will see more about Java and Related programming problems and solutions.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.