This Java program checks if a given number entered by the user is a Fibonacci number. It defines two helper functions: is_perfect_square() and isfibonacci().
Overall, this program checks if a given number is a Fibonacci number by calling the isfibonacci() function and displays the result.
import java.util.*; class Fibonacci { static boolean is_perfect_square(int n) { int sq = (int) Math.sqrt(n); return (sq*sq == n); } static boolean isfibonacci(int n) { return is_perfect_square(5*n*n + 4) || is_perfect_square(5*n*n - 4); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the Number : "); int num = input.nextInt(); if (num > 0) { System.out.println("Is Fibonacci Number ? "+isfibonacci(num)); } } }
Enter the Number : 5 Is Fibonacci Number ? true Enter the Number : 10 Is Fibonacci Number ? false
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions