- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Iterative Constructs in Java
Chapter 9
Iterative Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Check Spy Number
23. Write a program to accept a number and check and display whether it is a spy number or not. (A number is called a spy number if the sum of its digits equals the product of the digits.)
Example:
Consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8.
Product of the digits = 1 * 1 * 2 * 4 = 8.
Program Title: Check Spy Number
import java.util.Scanner;
public class SpyNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int temp = num;
int sum = 0;
int product = 1;
while (temp != 0) {
int digit = temp % 10;
sum += digit;
product *= digit;
temp /= 10;
}
System.out.println("Sum of digits: " + sum);
System.out.println("Product of digits: " + product);
if (sum == product) {
System.out.println(num + " is a Spy Number.");
} else {
System.out.println(num + " is NOT a Spy Number.");
}
sc.close();
}
}
Output
Sample Input / Output 1: Enter a number: 1124 Sum of digits: 8 Product of digits: 8 1124 is a Spy Number. Sample Input / Output 2: Enter a number: 123 Sum of digits: 6 Product of digits: 6 123 is a Spy Number.
📝 Explanation
- Spy Number Definition: A number where
- Sum of digits = Product of digits
- Extract digits using % 10
- Add to sum and multiply to product
- Compare sum and product to determine if it’s a Spy Number
