Fibonacci Series in Java

Find the Fibonacci Series up to Nth Term in Java

Find the Fibonacci Series up to Nth Term in Java

For a Given integer input number as the Nth value, the objective is to Find the Fibonacci Series up to the Nth Term. Therefore, we’ll write a program to Find the Fibonacci Series up to Nth Term in Java Language.

Example
Input : 4
Output : 0 1 1 2

Find the Fibonacci Series up to Nth Term in Java Language

Given an integer input for the Nth value, the objective is to Find the Fibonacci Series up to the Nth term using Loops and recursion in Java Language. To do so we’ll use the following methods,

  • Method 1: Using Iteration
  • Method 2: Using Recursion
  • Method 3: Using Static Variable
  • Method4: Using Direct Formula

We’ll discuss the above mentioned methods in detail in the upcoming sections. For better understanding, checkout the section below.

Method 1: Using Iteration

Java Code

Run
public class Main
 {
   public static void main (String[]args)
   {

     int num = 15;
     int a = 0, b = 1;

     // Here we are printing 0th and 1st terms
       System.out.print (a + " , " + b + " , ");

     int nextTerm;

     // printing the rest of the terms here
     for (int i = 2; i < num; i++)
       {
      nextTerm = a + b;
      a = b;
          b = nextTerm;
          System.out.print (nextTerm + " , ");
       }


   }
 }

Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Method 2: Using Recursion

Java Code

Run
public class Main
 {
   static int a = 0, b = 1, nextTerm;
   public static void main (String[]args)
   {

     int n = 15;
     // Here we are printing 0th and 1st terms
       System.out.print (a + " , " + b + " , ");

     // n-2 as 2 terms already printed
       Fib (n - 2);
   }

   static int Fib (int n)
   {
     if (n > 0)
       {
      nextTerm = a + b;
      a = b;
      b = nextTerm;

      System.out.print (nextTerm + " , ");
      Fib (n - 1);
       }
     return 0;

   }

 }

Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Method 3: Using Static Variable

Java Code

Run
public class Main
{
  static int a = 0, b = 1, nextTerm;
  public static void main (String[]args)
  {

    int n = 15;

      fib (n);
  }

  static void fib (int n)
  {

    int f[] = new int[n + 1];

    f[0] = 0;
    f[1] = 1;

    System.out.print (f[0] + " , " + f[1] + " , ");

    for (int i = 2; i < n; i++)
      {
	    f[i] = f[i - 1] + f[i - 2];
    	System.out.print (f[i] + " , ");
      }
  }

}

Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Method 4: Using Direct Formula

Java Code

Run
public class Fibonacci {
    public static void getFibo(double phi, int n) {
        for (int i = 0; i <= n; i++) {
           
            System.out.print(Math.round(Math.pow(phi, i) / Math.sqrt(5)) + ", ");
        }
    }

    public static void main(String[] args) {
        int n = 15;
        double phi = (1 + Math.sqrt(5)) / 2;
        getFibo(phi, n);
    }
}


Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

6 comments on “Fibonacci Series in Java”


  • Rupesh

    public class problem {
    static int a = 0, b = 1, nextnum;

    public static void main(String[] args) {
    int num = 20;
    System.out.print(a + “,” + b + “,”);
    fib(num);

    }

    static int fib(int num) {
    if (num – 2 > 0) {
    int nextnum = a + b;
    a = b;
    b = nextnum;
    System.out.print(“,” + nextnum);
    fib(num – 1);

    }
    return 0;

    }
    }


  • Sayani

    import java.util.*;

    public class fibbonacci_series {
    public static void main(String args[])
    {
    int a = 0;
    int b = 1;
    int value = 5;

    int store;
    System.out.print(a + ” ” + b + ” “);
    for(int i = 2 ; i < value ; i++)
    {
    store = a + b;
    a = b;
    b = store;
    System.out.print(store+" ");
    }

    }
    }


  • Debyanshu

    public class custome_Stack {
    public static void main(String[] args){

    int a=5;
    fib(a);
    }
    static void fib(int n){
    int a=0;
    int b=1;
    int c;
    for (int i = 0; i <n ; i++) {
    System.out.print(a+" ");
    c=a+b;
    a=b;
    b=c;

    }
    }

    }


  • Ravi Kant

    public class Fibonacci {
    public static void main(String[] args) {
    int n1=0, n2=1, n3;
    System.out.println(n1+”add”+n2);
    for (int i=0; i<7; i++){
    n3=n1+n2;
    System.out.println(""+n3);
    n1=n2;
    n2=n3;
    }
    System.out.println("print fibonacci no:"+n2);
    }
    }


  • Rakesh Deore

    public class Fibo
    {
    public static void main (String [] args)
    {

    int num = 8;
    int a = 0, b = 1, c;

    for(int i=0 ;i<=num; i++)
    {
    c=a+b;
    a=b;
    b=c;
    System.out.println(a);
    }
    }


  • Satendra

    class hallo{
    public static void fb(int a, int b, int n){
    if(n==0){
    return;
    }
    int c=a+b;
    System.out .println(c);
    fb( b,c, n-1);
    }
    public static void main (String[] args){
    int n=8,a=0,b=1;
    System.out.println(a);
    System.out.println(b);
    fb( a, b, n-2);
    } }