# Fibonacci series using Recursion in Java

Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers by definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.The Fibonacci sequence is named after Leonardo Fibonacci. –From Wiki

Fibonacci sequence examples

• 1,1,2,3,5,8,13,21,34,55,89,144 . . . . . [Starting with 1]
• 0,1,1,2,3,5,8,13,21,34,55,89,144. . . .[Starting with 0]

In mathematical terms, Recurrence relation or Algorithm can be defined as,

``````fib(n) = 0                    if(n=0)
= 1                    if(n=1)
= fib(n-1)+fib(n-2)    if(n>1)``````
``````fib(n) = 1                    if(n=1)
= 1                    if(n=2)
= fib(n-1)+fib(n-2)    if(n>2)``````

## Fibonacci Series implementation in Java

Here is the implementation for Fibonacci series using recursion in java

``````package com.jminded.recursion;

public class FibonacciNumberUsingRecursion {

/**
* @author Umashankar
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//fibonacci series --> 0,1,1,2,3,5,8,13. . . .
//fibonacciNumber(0) represents 0th fibonacci number, fibonacciNumber(6) represents 5th fibonacci number with zero based index. i.e., 8
System.out.println("Fibonacci Series for 10numbers");
int fibonacciSeriesLength=10;
for(int i=0;i<fibonacciSeriesLength;i++){
System.out.print(fibonacciNumber(i)+",");
}
}
/**
* <p>Fibonacci Number using Recursion</p>
* fib(n)    = 0  if(n=0)
*           = 1  if(n=1)
*           = fib(n-1)+fib(n-2)
* @param n
* @return
*/
public static int fibonacciNumber(int n){
//fibonacci series --> 0,1,1,2,3,5,8,13. . . .
//base cases
if(n==0){
return 0;
}else if(n==1){
return 1;
}else{   //recursive case
return fibonacciNumber(n-1)+fibonacciNumber(n-2);
}
}

}``````

## Output:

``````Fibonacci Series for 10numbers
0,1,1,2,3,5,8,13,21,34,``````

### 1 thought on “Fibonacci series using Recursion in Java”

1. Hi,
Check here example for recursive and iterative Fibonacci in Java

Eyal