
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program for check if a given number is Fibonacci number?
Following is the Java program to check if a given number is Fibonacci −
Example
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"); } }
Output
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
A class named Demo defines a static Boolean function that takes in an integer value as parameter. It checks the square root of the value and assigns it to another value. If the product of square root multiplied by square root is equal to the value passed, then it is returned.
Next, another Boolean static function is defined that calls the previous function. In the main function, the starting number, and the ending number are iterated through, and relevant message is printed as well as checking whether every number is a Fibonacci number or not.
- Related Articles
- Python Program for How to check if a given number is a Fibonacci number?
- How to check if a given number is a Fibonacci number in Python Program ?
- Java Program to Check if a given Number is Perfect Number
- Program to check given number is a Fibonacci term in Python
- Java Program for n-th Fibonacci number
- Java program to print Fibonacci series of a given number.
- C program to find Fibonacci series for a given number
- Python program to check if the given number is a Disarium Number
- Python program to check if the given number is Happy Number
- Java Program for nth multiple of a number in Fibonacci Series
- Check whether a number is a Fibonacci number or not JavaScript
- Java program to check whether the given number is an Armstrong number
- Java Program To Reverse A Number And Check If It Is A Palindrome Number
- Python program to check if a given string is number Palindrome
- Java Program to check if a string is a valid number

Advertisements