In the previous article, we have seen Java Program to Calculate Power of a Number
In this article we are going to see how to check if a given number is Fibonacci number or not using Java programming language. Fibonacci series in java using scanner, fibonacci series in given range in java, member of fibonacci flowchart discussed in the given below content.
Java Program to Check if a Given Number is Fibonacci Number or Not
Before Jumping into the program directly let’s see how to check if a given number is Fibonacci number or not.
Explanation:
The Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ….. Starts from 0 and 1 and then next onwards next number will be the sum of previous 2 numbers. If any number which is present inside this series is called a Fibonacci number. To check a number is Fibonacci number or not : Let N be a Fibonacci number, if and only if ( (5*N*N) + 4 ) or ( (5*N*N) – 4 ) is a perfect square.
Example:
3 is a Fibonacci number since (5*3*3 + 4) is 49 which is 7*7 5 is a Fibonacci number since (5*5*5 – 4) is 121 which is 11*11 4 is not a Fibonacci number since neither (5*4*4 + 4) = 84 nor (5*4*4 – 4) = 76 are perfect squares.
Let’s see different ways to check if a given number is Fibonacci number or not.
Method-1: Java Program to Check if a Given Number is Fibonacci Number or Not By Using Static Input Value
Approach:
- Declare an integer variable say “
n
” which will store the number to check. - Declare a method called
checkFibonacci()
which will return Boolean output to the main method. - Then declare a
checkPerfectSquare()
method which will return Boolean output to thecheckFibonacci()
method - If the Boolean output returned by the
checkFibonacci()
method istrue
then Print the result as Fibonacci number, else print not a Fibonacci number.
Program:
import java.util.*; class Main { public static void main(String [] args) { // A number declared int num = 1; //Calling checkFibonacci() user defined method inside the condition //if the checkFibonacci() returns true then print the number as a Fibonacci number //else print it is not a Fibonacci number if (checkFibonacci(num) == true) System.out.println(num + " is a Fibonacci number"); else System.out.println(num + " is not a Fibonacci number"); } //checkFibonacci() user defined method to check number is Fibonacci or not static boolean checkFibonacci(int num) { //Calling checkPerfectSquare() method and passing the parameter //after geting the value by using the formula //this method returns true if it is perfect square else returns false return checkPerfectSquare(5*num*num + 4) || checkPerfectSquare(5*num*num - 4); } //checkPerfectSquare() user defined method to check number is Perfect Square or not. static boolean checkPerfectSquare(int x) { int s = (int) Math.sqrt(x); return (s*s == x); } }
Output: 8 is a Fibonacci number
Method-2: Java Program to Check if a Given Number is Fibonacci Number or Not By Using User Input Value
Approach:
- Declare an integer variable say “
n
” and take the value as input from user. - Declare a method called
checkFibonacci()
which will return Boolean output to the main method. - Then declare a
checkPerfectSquare()
method which will return Boolean output to thecheckFibonacci()
method - If the Boolean output returned by the
checkFibonacci()
method istrue
then Print the result as Fibonacci number, else print not a Fibonacci number.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object created Scanner sc = new Scanner(System.in); //Taking a number input from user System.out.println("Enter a number:"); int num = sc.nextInt(); //Calling checkFibonacci() user defined method inside the condition //if the checkFibonacci() returns true then print the number as a Fibonacci number //else print it is not a Fibonacci number if (checkFibonacci(num) == true) System.out.println(num + " is a Fibonacci number"); else System.out.println(num + " is not a Fibonacci number"); } //checkFibonacci() user defined method to check number is Fibonacci or not static boolean checkFibonacci(int num) { //Calling checkPerfectSquare() method and passing the parameter //after geting the value by using the formula //this method returns true if it is perfect square else returns false return checkPerfectSquare(5*num*num + 4) || checkPerfectSquare(5*num*num - 4); } //checkPerfectSquare() user defined method to check number is Perfect Square or not. static boolean checkPerfectSquare(int x) { int s = (int) Math.sqrt(x); return (s*s == x); } }
Output: Case-1 Enter a number: 8 8 is a Fibonacci number Case-2 Enter a number: 7 7 is not a Fibonacci number
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Test yourself:
- Write a program to check whether given number is fibonacci number or not python?
- Write a java program to check if a given number is fibonacci number or not?
- How to check if a given number is fibonacci number in javascript?
- C program to check whether a number is fibonacci or not?
- How to check number is fibonacci or not?
- How to check whether a number is fibonacci or not in c?
Related Java Programs: