Home / Programs / Check if a Number is a Fibonacci Number in Range using Java
🚀 Programming Example

Check if a Number is a Fibonacci Number in Range using Java

👁 175 Views
💻 Practical Program
📘 Step Learning

📌 Information & Algorithm

Given Input:

range [1, 100]
55

Expected Output:

55 is a Fibonacci number within the range [1, 100].
Fibonacci numbers within the range [1, 100]: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

💻 Program Code

import java.util.*;

public class FibonacciInRangeChecker {

    // Function to check if a number is a perfect square
    public static boolean isPerfectSquare(int x) {
        int s = (int) Math.sqrt(x);
        return s * s == x;
    }

    // Function to check if a number is a Fibonacci number
    public static boolean isFibonacci(int number) {
        // A number is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both is a perfect square
        return isPerfectSquare(5 * number * number + 4) || isPerfectSquare(5 * number * number - 4);
    }

    // Function to find all Fibonacci numbers in a given range
    public static List<Integer> findFibonacciNumbersInRange(int start, int end) {
        List<Integer> fibonacciNumbers = new ArrayList<>();

        int currentFibonacci = 0;
        int n1 = 0, n2 = 1;

        while (currentFibonacci <= end) {
            if (currentFibonacci >= start) {
                fibonacciNumbers.add(currentFibonacci);
            }

            // Calculate next Fibonacci number
            currentFibonacci = n1 + n2;
            n1 = n2;
            n2 = currentFibonacci;
        }

        return fibonacciNumbers;
    }

    public static void main(String[] args) {
        int start = 1; // Example start of range
        int end = 100; // Example end of range

        List<Integer> fibonacciNumbersInRange = findFibonacciNumbersInRange(start, end);

        // Check if a specific number is Fibonacci and within the range
        int numberToCheck = 55; // Example number to check
        if (fibonacciNumbersInRange.contains(numberToCheck) && isFibonacci(numberToCheck)) {
            System.out.println(numberToCheck + " is a Fibonacci number within the range [" + start + ", " + end + "].");
        } else {
            System.out.println(numberToCheck + " is not a Fibonacci number within the range [" + start + ", " + end + "].");
        }

        // Print all Fibonacci numbers found within the range
        System.out.println("Fibonacci numbers within the range [" + start + ", " + end + "]: " + fibonacciNumbersInRange);
    }
}

                        

🖥 Program Output

55 is a Fibonacci number within the range [1, 100].
Fibonacci numbers within the range [1, 100]: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
                            

📘 Explanation

  • isPerfectSquare(int x): Helper function to check if x is a perfect square.
  • isFibonacci(int number): Function to check if number is a Fibonacci number using the mathematical property discussed earlier.
  • findFibonacciNumbersInRange(int start, int end): Function to find all Fibonacci numbers within the specified range [start, end]. It iterates through Fibonacci numbers until the current Fibonacci number exceeds end, storing those that fall within the range.
  • main(String[] args): In the main method, specify the start and end of the range. It calculates all Fibonacci numbers in the range and checks if a specific numberToCheck is both a Fibonacci number and within the range.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.