Java Program Print Fibonacci Series

Java Program Print Fibonacci Series

Java Program Print Fibonacci Series asked in many interview where java coding was mandatory. Below is sample program:

package com.javahonk.test;

public class FibonacciTest {

	public static void main(String[] args) {

		int n = 30;
		int f = 0, g = 1;

		for (int i = 1; i <= n; i++) {
			f = f + g;
			g = f - g;
			System.out.println(f);
		}

		for (int i = 1; i <= n; i++) {
			System.out.println(fibonacciRecusion(i));
		}

	}

	public static int fibonacciRecusion(int number) {

		if (number == 1 || number == 2) {
			return 1;
		}

		return fibonacciRecusion(number - 1) + fibonacciRecusion(number - 2);
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *