JAVA PROGRAM TO FIND FIBONACCI SERIES

Fibonacci Series in Java is a series of numbers where the number is the sum of the next two terms . The first two numbers/terms are 0 followed by 1 and so on.

Assume you have to find the Fibonacci series of a number say 8, then Fibonacci series be calculated as 8 = 0 , 1 , 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), 13 (5+8).

Table of Contents

[hide]
  • Java Program to generate fibonacci series of a number using for loop
  • Java Program to generate fibonacci series of a number using while loop
  • Java Program to generate fibonacci series using recursion in java
  • Java Program to generate fibonacci series using recursion in java(Another way)

Example 1 : Java Program to generate fibonacci series of a number using for loop

copyimage
//Java Program to generate fibonacci series of a number using for loop
import java.util.*;
class FibonacciDemo{
    public static void findfibonacci(int n){
	int first=0;
	int second=1;
	int result=0;
	for(int i=1;i<n;i++) {
	   System.out.print(result+" ");
	   result = first+second;
	   first = second;
	   second = result;
	}
  }
}
public class Codebator{
    public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("Enter a number");
	int num=sc.nextInt();
	FibonacciDemo.findfibonacci(num);
   }
}

OUTPUT :

Enter any Number8 10 1 2 3 5 8 13 

Example 2 : Java Program to generate fibonacci series of a number using while loop

copyimage
//Java Program to generate fibonacci series of a number using while loop
import java.util.*;
class FibonacciDemo{
    public static void findfibonacci(int n){
	int i = 1 , first = 0, second = 1, result = 0;
         while(i <= n) {
         System.out.print(first + " ");
         result = first + second;
         first = second;
         second = result;
         i++;
      }
  }
}
public class Codebator{
    public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("Enter a number");
	int num=sc.nextInt();
	FibonacciDemo.findfibonacci(num);
   }
}

OUTPUT :

Enter any Number9 0 1 1 2 3 5 8 13 21 

Example 3 : Java Program to generate fibonacci series using recursion in java

copyimage
//Java Program to generate fibonacci series using recursion in java
import java.util.*;
class FibonacciDemo{
	static int i = 1 , first = 0, second = 1, result = 0;
  public static void findfibonacci(int n){
       if(n>0) {
       result = first + second;
       first = second;
       second = result;
       System.out.print(" "+result);
       findfibonacci(n-1);
    }
}
}
public class Codebator{
  public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("Enter a number");
	int num=sc.nextInt();
	System.out.print(FibonacciDemo.first+" "+FibonacciDemo.second); 
	FibonacciDemo.findfibonacci(num-2);
 }
}

OUTPUT :

Enter any Number10 0 1 1 2 3 5 8 13 21 34 

Example 4 : Java Program to generate fibonacci series using recursion in java(Another way)

copyimage
//Java Program to generate fibonacci series using recursion in java
import java.util.*;
class FibonacciDemo{
  public static int findfibonacci(int n){
	  if(n==0)
          return 0;
      else if(n==1)
          return 1;
      else
          return findfibonacci(n-1) + findfibonacci(n-2);
}
}
public class Codebator{
  public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("Enter a number");
	int num=sc.nextInt();
	for (int i = 0; i<=num-1; ++i)
    {
       System.out.print(FibonacciDemo.findfibonacci(i) + " ");
    }
 }
}

OUTPUT :

Enter any Number11 0 1 1 2 3 5 8 13 21 34 55