A spy number is a number where the sum of its digits is equal to the product of its digits. For example, the number 123 is a spy number because 1+2+3=6 and 1×2×3=6. This type of number is often used in mathematical puzzles and number theory problems.
This example demonstrates how to check if a given number is a spy number in Java. The program includes a method to calculate the sum and product of the digits of the number and then compares these two values.
import java.util.Scanner;
public class SpyNumberCheck {
// Method to check if a number is a spy number
public static boolean isSpyNumber(int number) {
int sum = 0;
int product = 1;
int originalNumber = number;
while (number != 0) {
int digit = number % 10;
sum += digit;
product *= digit;
number /= 10;
}
return sum == product;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isSpyNumber(number)) {
System.out.println(number + " is a spy number.");
} else {
System.out.println(number + " is not a spy number.");
}
scanner.close();
}
}
public class SpyNumberCheck {
This line defines a new class named SpyNumberCheck
. This class contains methods to check if a number is a spy number and the main method to execute the program.
public static boolean isSpyNumber(int number) {
This method isSpyNumber
takes an integer parameter number
and returns a boolean
indicating whether the number is a spy number.
int sum = 0;
int product = 1;
int originalNumber = number;
sum
is initialized to 0 and will store the sum of the digits.product
is initialized to 1 and will store the product of the digits.originalNumber
stores the original number for comparison purposes.while (number != 0) {
int digit = number % 10;
sum += digit;
product *= digit;
number /= 10;
}
while
loop extracts digits from the end of number
.digit
stores the last digit of the number.sum
is updated by adding the current digit.product
is updated by multiplying the current digit.number
is updated by removing the last digit (integer division by 10).return sum == product;
The method returns true
if the calculated sum equals the calculated product, indicating it is a spy number; otherwise, it returns false
.
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 + " ");
}
}
}
}
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