
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Given Number is a Fibonacci Number in Java
For a given input number, write a Java program to check if it is a Fibonacci number. Any number that belongs to Fibonacci series is called as Fibonacci number. The Fibonacci series is a sequence of numbers formed by the sum of its two previous integers. The first two terms of this series are 0 and 1 and further terms go like 1, 2, 3, 5, 8 and so on. This series was named after a famous Italian mathematician, Leonardo Fibonacci.
Example Scenario 1
Input: num1 = 8; Output: 8 is a Fibonacci number
8 comes into Fibonacci series
Example Scenario 2
Input: num2 = 9; Output: 9 is not a Fibonacci number
How to Check Fibonacci Number in Java?
Use the below approaches to check whether a given number is Fibonacci ?
- Iterative approach
- Mathematical approach
- Recursive approach
Using Iterative Approach
In the iterative approach, find Fibonacci numbers iteratively until the loop reaches or surpasses the given number. However, it is not efficient for large numbers due to its linear time complexity.
Program
In this Java program, we are using iterative approach to check whether the given input is Fibonacci number or not.
public class CheckerClass { // function to check fibonacci public static boolean fibonacci_num_check(int number) { if (number == 0 || number == 1) return true; int prev = 0, next = 1, sum; while (next < number) { sum = prev + next; prev = next; next = sum; } return number == next; } public static void main(String[] args) { // given input number int inputNum = 8; // checking if given number is fibonacci boolean isFibonacciNumber = fibonacci_num_check(inputNum); if (isFibonacciNumber) { System.out.println(inputNum + " is a Fibonacci number."); } else { System.out.println(inputNum + " is not a Fibonacci number."); } } }
On executing, this code will show below result ?
8 is a Fibonacci number.
Using Mathematical Approach
In this approach, calculate the perfect square to check whether the given number is Fibonacci or not. A given number is said to be Fibonacci number if and only if one or both of (5 times n^2 + 4) or (5 times n^2 - 4) is a perfect square.
Program
Following is the Java program to check if a given number is Fibonacci with the help of the above discussed Mathematical approach.
public class Demo{ static boolean perfect_square_check(int val){ int s = (int) Math.sqrt(val); return (s*s == val); } static boolean fibonacci_num_check(int n){ return perfect_square_check(5*n*n + 4) || perfect_square_check(5*n*n - 4); } public static void main(String[] args){ for (int i = 6; i <= 17; i++) System.out.println(fibonacci_num_check(i) ? i + " is a Fibonacci number" : i + " is a not Fibonacci number"); } }
On running the code, following output will be displayed ?
6 is a not Fibonacci number 7 is a not Fibonacci number 8 is a Fibonacci number 9 is a not Fibonacci number 10 is a not Fibonacci number 11 is a not Fibonacci number 12 is a not Fibonacci number 13 is a Fibonacci number 14 is a not Fibonacci number 15 is a not Fibonacci number 16 is a not Fibonacci number 17 is a not Fibonacci number
Using Recursive Approach
To check Fibonacci number recursively, follow the given steps −
- Step 1: Calculate the n-th Fibonacci number recursively.
- Step 2: Compare the calculated Fibonacci with the given input.
- Step 3: If both numbers are equal, return TRUE otherwise FALSE.
Program
The following Java program demonstrates how to check Fibonacci number using recursive approach.
public class CheckerClass { // calculate n-th Fibonacci number recursively public static int fibonacci_num_check(int n) { if (n <= 1) return n; return fibonacci_num_check(n-1) + fibonacci_num_check(n-2); } // check Fibonacci number public static boolean checkFibonacci(int number) { int fib = 0; int i = 0; while ((fib = fibonacci_num_check(i)) < number) { i++; } return number == fib; } public static void main(String[] args) { // given input number int inputNum = 21; // checking if given number is fibonacci boolean isFibonacciNumber = checkFibonacci(inputNum); if (isFibonacciNumber) { System.out.println(inputNum + " is a Fibonacci number."); } else { System.out.println(inputNum + " is not a Fibonacci number."); } } }
On running the code, following output will be displayed −
21 is a Fibonacci number.