Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Write a program to accept a number and check and display whether it is a spy number or not. (A number is spy if the sum of its digits equals the product of its digits.)
Example: consider the number 1124, Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 x 1 x 2 x 4 = 8
Answer:
import java.util.Scanner;
public class SpyNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int n = scanner.nextInt();
int sumOfDigits = 0;
int productOfDigits = 1;
while (n > 0) {
int remainder = n % 10;
sumOfDigits = sumOfDigits + remainder;
productOfDigits = productOfDigits * remainder;
n = n / 10;
}
if (sumOfDigits == productOfDigits) {
System.out.println("Spy number");
} else {
System.out.println("Not a spy number");
}
}
}
Sample output 1
Enter number: 1124
Spy number
Sample output 2
Enter number: 34
Not a spy number
C. Sir| Created: 15-Apr-2019 | Updated: 15-Apr-2019|ICSE2017