print
Advertisment
Advertisment

spy number program in java

Here is an example of spy number program in java:

SpyNumberChecker.java
import java.util.Scanner;
public class SpyNumberChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); // Create a Scanner object for user input
        System.out.print("Enter a number: ");
        int number = input.nextInt(); // Get the number from the user

        // Find the sum of the digits of the number
        int sum = 0;
        int product = 1;
        while (number > 0) {
            int digit = number % 10;
            sum += digit;
            product *= digit;
            number /= 10;
        }

        // Check if the number is a spy number
        if (sum == product) {
            System.out.println(number + " is a spy number."); // If the number is a spy number, print a message to the console
        } else {
            System.out.println(number + " is not a spy number."); // If the number is not a spy number, print a message to the console
        }
    }
}
Advertisment

Output :

Enter a number to check if it is a spy number: 123
123 is not a spy number.
Enter a number to check if it is a spy number: 22
22 is a spy number.

Note that 22 is a "spy number" because the sum and product of its digits are both equal to 4.

Let's go through spy number program in java line by line:

  • import java.util.Scanner;: This imports the Scanner class from the java.util package, which allows us to read user input from the console.
  • public class SpyNumberChecker: This declares a new public class called SpyNumberChecker.
  • public static void main(String[] args): This declares a main method, which is where the program starts executing. It takes an array of strings as an argument, which is not used in this program.
  • Scanner input = new Scanner(System.in);: This creates a new Scanner object called input that reads from the standard input stream (i.e. the console).
  • System.out.print("Enter a number: ");: This prints a message to the console asking the user to enter a number.
  • int number = input.nextInt();: This reads an integer input from the user using the nextInt method of the Scanner object and stores it in the number variable.
  • int sum = 0;: This initializes a variable called sum to 0, which we will use to store the sum of the digits of the number.
  • int product = 1;: This initializes a variable called product to 1, which we will use to store the product of the digits of the number.
  • while (number > 0) {: This starts a while loop that will continue as long as the number variable is greater than 0.
  • int digit = number % 10;: This calculates the last digit of the number variable by taking the remainder when it is divided by 10.
  • sum += digit;: This adds the last digit to the sum variable.
  • product *= digit;: This multiplies the last digit with the product variable.
  • number /= 10;: This removes the last digit from the number variable by integer division with 10.
  • }: This ends the while loop.
  • if (sum == product) {: This starts an if statement that checks whether the sum variable is equal to the product variable. If they are equal, then the number is a "spy number."
  • System.out.println(number + " is a spy number.");: If the number is a "spy number," we print a message to the console that says the number is a spy number.
  • } else {: If the sum variable is not equal to the product variable, then the number is not a "spy number." We start an else block.
  • System.out.println(number + " is not a spy number.");: We print a message to the console that says the number is not a spy number.
  • }: We end the if-else statement.

When the program is run, it prompts the user to enter a number. The program then calculates the sum and product of the digits of the number and checks if they are equal. If the sum is equal to the product, the program prints a message to the console saying that the number is a "spy number." If the sum is not equal to the product, the program prints a message to the console saying that the number is not a "spy number."

Advertisment
Advertisment
arrow_upward