Spy number

A number is known as a spy number if the sum of its digits is exactly equal to the product of its digits.

Check Spy Number

This code checks if the given number is a “spy number.” A spy number is a positive integer where the sum and product of its digits are equal. The code first takes a number from the user and passes it to the checkSpy method, which separates each digit of the number and calculates the sum and product of its digits. If the sum and product are equal, the checkSpy method returns true, indicating that the number is a spy number. If the sum and product are not equal, the method returns false, indicating that the number is not a spy number. Finally, the main method prints the result of whether the number is a spy number or not.

import java.util.Scanner;

public class SpyNumber {
	private static boolean checkSpy(int number) {
		int sum = 0, mul = 1, rem;

		while (number > 0) {
			rem = number % 10;
			sum += rem;
			mul *= rem;
			number = number / 10;
		}
		if (sum == mul) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int number;

		System.out.print("Enter a number to check for spy number: ");
		number = reader.nextInt();
		reader.close();

		if (checkSpy(number)) {
			System.out.println(number + " is a spy number");
		} else {
			System.out.println(number + " is not a spy number");
		}
	}
}
Output
Enter a number to check for spy number: 123
123 is a spy number

Spy Number List (1-n)

This Java code checks a given range of numbers to see if they are spy numbers or not. A spy number is a number where the sum of its digits is equal to the product of its digits. The user is asked to input the upper bound for the range of numbers to be checked. The checkSpy function takes an integer number as input and checks if it’s a spy number or not by dividing it into its individual digits, summing them, and multiplying them. The main function calls checkSpy for each number in the given range and prints the spy numbers.

import java.util.Scanner;

public class SpyNumberSeries {
	private static boolean checkSpy(int number) {
		int sum = 0, mul = 1, rem;

		while (number > 0) {
			rem = number % 10;
			sum += rem;
			mul *= rem;
			number = number / 10;
		}
		if (sum == mul) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int max;

		System.out.print("Enter the upper bound to find spy numbers: ");
		max = reader.nextInt();
		reader.close();


		System.out.println("Spy numbers:");
		for (int i = 1; i <= max; i++) {
			if (checkSpy(i)) {
				System.out.print(i + " ");
			}
		}
	}
}
Output
Enter the upper bound to find spy numbers: 2000
Spy numbers:
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421