In mathematics, the **Fibonacci numbers** or **Fibonacci series** or **Fibonacci sequence** are the numbers in the following integer sequence:

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence *F _{n}* of Fibonacci numbers is defined by the recurrence relation

with seed values^{
}

Here is a simplest Java Program to generate Fibonacci Series.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.crunchify.tutorials; import java.util.Scanner; /** * @author Crunchify.com */ public class CrunchifyFibonacci { @SuppressWarnings("resource") public static void main(String args[]) { //input to print Fibonacci series upto how many numbers System.out.print("Enter number upto which Fibonacci series to print: "); int number = new Scanner(System.in).nextInt(); System.out.println("\n\nFibonacci series upto " + number +" numbers : "); //printing Fibonacci series upto number for(int i=1; i<=number; i++){ System.out.print(fibonacciRecusion(i) +" "); } } // Java program for Fibonacci number using recursion. public static int fibonacciRecusion(int number){ if(number == 1 || number == 2){ return 1; } return fibonacciRecusion(number-1) + fibonacciRecusion(number -2); //tail recursion } // Java program for Fibonacci number using Loop. public static int fibonacciLoop(int number){ if(number == 1 || number == 2){ return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number } } |

**Output:**

1 2 3 4 |
Enter number upto which Fibonacci series to print: 15 Fibonacci series upto 15 numbers : 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 |

Hi I like this style, to avoid errors with negative numbers.

public static Integer getFibonacci(int x) {

if (x <= 0)

return 0;

if (x == 1)

return 1;

return getFibonacci(x – 1) + getFibonacci(x – 2);

}