Home / Programs / Java Program to Check Spy Number
🚀 Programming Example

Java Program to Check Spy Number

👁 86 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

Given Input:

 int[] testNumbers = {112, 123, 22, 132};

Expected Output:

Checking Spy Numbers:
112 is NOT a Spy Number.
123 is a Spy Number!
22 is a Spy Number!
132 is a Spy Number!
Press any key to continue . . .

💻 Program Code

public class SpyNumberChecker {

    // Method to check if a number is a Spy Number
    public static boolean isSpyNumber(int number) {
        int sum = 0, product = 1, digit;

        // Extract digits and calculate sum and product
        while (number > 0) {
            digit = number % 10; // Get the last digit
            sum += digit;       // Add to sum
            product *= digit;   // Multiply to product
            number /= 10;       // Remove the last digit
        }

        // Check if sum equals product
        return sum == product;
    }

    public static void main(String[] args) {
        // Test numbers
        int[] testNumbers = {112, 123, 22, 132};

        System.out.println("Checking Spy Numbers:");
        for (int num : testNumbers) {
            if (isSpyNumber(num)) {
                System.out.println(num + " is a Spy Number!");
            } else {
                System.out.println(num + " is NOT a Spy Number.");
            }
        }
    }
}

                        

🖥 Program Output

Checking Spy Numbers:
112 is NOT a Spy Number.
123 is a Spy Number!
22 is a Spy Number!
132 is a Spy Number!
Press any key to continue . . .

                            
📚 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.