Java Program to check Fibonacci Series


Introduction


In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones.

Example:

0,1,1,2,3,5,8,13,21,34,……………………

Formula:

Tn = T(n-1)+T(n-2) Where n >2 and T1=0 & T1 = 1

This program helps to check that entered number belongs to Fibonacci Series or not.

 


Flowchart


Please find sample flowchart diagram for this program as given below –

 

 

 


Program (Code)


package com.kw.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This program helps to check Fibonacci Number. 0,1,1,2,3,5,8,13,21......
 *
 * @author dsahu1
 *
 */
public class KWIsFibonacciNumber {

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        // Call method to validate number for Fibonacci
        isFibonacciNumber();
    }

    /**
     * This method helps to validate number as Fibonacci or not.
     *
     * @throws NumberFormatException
     * @throws IOException
     */
    public static void isFibonacciNumber() throws NumberFormatException,
            IOException {

        // Instantiate BufferedReader object
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a number : ");
        // Enter number
        int num = Integer.parseInt(br.readLine());

        if (num < 0) {
            System.out.println("Please enter correct number!");
        } else {
            // Attribute to hold First Term of Fibonacci Series
            long firstTerm = 0;
            // Attribute to hold Second Term of Fibonacci Series
            long secondTerm = 1;
            // Attribute to hold Next Term of Fibonacci Series
            long nextTerm = 0;

            if (num == firstTerm || num == secondTerm) {
                System.out.println("The number belongs to Fibonacci Series.");
            } else {
                while (nextTerm < num) {
                    // Calculate Next Term of the series
                    nextTerm = firstTerm + secondTerm;
                    // Assign second term to first term
                    firstTerm = secondTerm;
                    // assign next term to second term to get next term
                    secondTerm = nextTerm;
                }
                // Compare number with the last calculated number
                if (nextTerm == num) {
                    System.out
                            .println("The number belongs to Fibonacci Series.");
                } else {
                    System.out
                            .println("The number does not belongs to Fibonacci Series.");
                }
            }

        }

    }

}

 


Output


Scenario 1:

Enter a number : 13
The number belongs to Fibonacci Series.

——————————————-

Enter a number : 21
The number belongs to Fibonacci Series.

—————————-

Enter a number : 46368
The number belongs to Fibonacci Series.

 

Scenario 2:

Enter a number : 46369
The number does not belongs to Fibonacci Series.

—————————————————

Enter a number : 158564
The number does not belongs to Fibonacci Series.

 


Video Explanation


This video helps to understand flowchart and program which written in the above steps. There are various ways to write a program and we are not covering all of them as this is just for practice. Please post your program in the comment box so other can utilize it.

 

Please Find Video below –

One thought on “Java Program to check Fibonacci Series”

Leave a Reply

Your email address will not be published. Required fields are marked *