Java program to print Fibonacci series of a given number.

Java Programming Java8Java Technologies

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Example

Following is an example to find Fibonacci series of a given number using a recursive function

public class FibonacciSeriesUsingRecursion {
   public static long fibonacci(long number) {
      if ((number == 0) || (number == 1)) return number;
         else return fibonacci(number - 1) + fibonacci(number - 2);
      }
      public static void main(String[] args) {
         for (int counter = 0; counter <= 10; counter++){
            System.out.print(" "+fibonacci(counter));
         }
      }
   }

Output

0 1 1 2 3 5 8 13 21 34 55
raja
Published on 25-Apr-2018 14:00:44
Advertisements