Fibonacci Series in java

Fibonacci Series in java
Introduction

Fibonacci Series in java with example


The Fibonacci series or numbers denoted by Fn. It is a series of numbers formed by the addition of the preceding two numbers in the series. The first two terms are 0 and 1 respectively. The terms after this are generated by simply adding the previous two terms.
Like f1=0, fib=0, f2=1 . Here we add f1=f2, f2=fib and fib=f1+f2
f1=1 as f2 value is 1
f2=0 as fib value is 0
fib=f1+f2 as 1 after adding f1 and f2, and it will goes on till nth of the number.

For example of Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13….etc.

In this article, I have given four ways to print the Fibonacci series in java.

  1. Fibonacci Series Using For Loop
  2. Fibonacci Series Using while Loop
  3. Fibonacci Series Using Big number
  4. Fibonacci Series Using Recursion

Fibonacci Series Using For Loop

In this program, I have generated the Fibonacci series using for loop. I have taken a number in n and run a for a loop.

import java.util.Scanner;
public class FibonacciSeriesUsingForLoop
{
    public static void main(String arg[]) 
    {
        Scanner sc = new Scanner(System.in);
        int f1, f2, fib, i, n;
        System.out.println("Enter a number Print Fibonacci Series:");
        n = sc.nextInt();
        f1 = fib = 0;
        f2 = 1;
        System.out.print(fib+" ");
        for(i=0; i<n; i++)
        {
            f1 = f2;
            f2 = fib;
            fib = f1 + f2;
            System.out.print(fib+" ");
        }
    }
}

Fibonacci Series Using For Loop

In the following program, I have used a while to print the Fibonacci number. 
import java.util.*;
public class FibonacciSeriesUsingWhileLoop
{
    public static void main(String arg[]) 
    {
        Scanner sc = new Scanner(System.in);
        int f1, f2, fib, i, num;
        System.out.println("Enter a number Print Fibonacci Series:");
        num = sc.nextInt();
        f1 = fib = 0;
        f2 = 1;
        i=0;
        System.out.print(fib+" ");
        while(i < num)
        {
            f1 = f2;
            f2 = fib;
            fib = f1 + f2;
            System.out.print(fib+" ");
            i++;
        }
    }
}


Fibonacci Series Using Big number in Java

The Fibonacci numbers can grow exponentially and it could be very large numbers. With the help of primitive data types, it is not possible to generate huge or big numbers such as 600 or more. To handle such a big number, we use BigInteger which can easily handle large numbers.

import java.math.BigInteger;
import java.util.*;
public class FibonacciSeriesBigNumber
{
    // BigInteger class objects
    BigInteger f1 = BigInteger.ZERO;
    BigInteger f2 = BigInteger.ONE; 
    BigInteger fib = BigInteger.ONE; 
    void fibonacci(int n)
    {
        System.out.print(f1 + " ");
        for (int i=2 ; i<=n ; i++)
        { 
            fib =  f1.add(f2); 
            f1 = f2; 
            f2 = fib; 
            System.out.print(fib + " ");
        }           
    }
    public static void main(String args[]) 
    {
        FibonacciSeriesBigNumber ob = new FibonacciSeriesBigNumber();
        int n;
        Scanner sc  = new Scanner(System.in);
        System.out.println("Enter a number :");
        n = sc.nextInt();
        // Call the method
        ob.fibonacci(n);        
    } 
}
Here is a sample output of 50
Enter a number :
50
0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025

Fibonacci Series Using Recursion

A Recursion is a function which calls itself directly or indirectly. This enables the function to repeat itself several times, outputting the result and the end of each iteration.
A class FibonacciSeriesUsingRecursion has been defined to find the Fibonacci series up to an nth limit. Some of the members of the class are given below:
Class Name : FibonacciSeriesUsingRecursion
Data Members/Local variables: int n, i, num;
Member functions/methods :
Method: fiboSeries(n)- calculate Fibonacci series.

Let’s see how to use recursion to print first ‘n’ numbers of the Fibonacci Series in Java.
The recursive method executes a little slower than the iterative method. Many recursive calls to a method could cause a stack overrun. It is because of storage for parameters and local variables, it is possible that the stack could be exhausted. If this occurs, the Java run-time system will cause an exception. With the help of the recursive method, most of the algorithms become simpler and clearer than the iterative method. For example, the QuickSort sorting algorithm is quite difficult to implement iteratively.  Fibonacci is simple in the Recursive method.


Here is the program

import java.util.*;
public class FibonacciSeriesUsingRecursion
{
    // Fibonacci series method with recursion technique
    public static int fiboSeries(int n)
    {

        if(n==0)
            return 0;
        else if(n==1)
            return 1;
        else
            return fiboSeries(n-1) + fiboSeries(n-2);

    } 
    // Main method
    public static void main(String[] args) 
    {

        int n, i, num;

        System.out.println("Enter n terms for Fibonacci Series");
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();

        for (i = 0; i<=n-1; ++i)
        {
            num = fiboSeries(i); // static method call
            System.out.print(num + " ");
        }
    }
}


SHARE THIS
Previous Post
Next Post