C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Spy Number Program - Java Programs

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 × 1 × 2 × 4 = 8

import java.util.Scanner; class Spy{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the number: "); int n = Integer.parseInt(in.nextLine()); int sum = 0; int product = 1; for(int i = n; i != 0; i /= 10){ sum += i % 10; product *= i % 10; } if(sum == product) System.out.println("Spy number"); else System.out.println("Not a Spy Number"); } }

Output

 
 OUTPUT 1: 
Enter the number: 1124
Spy number 

 OUTPUT 2: 
Enter the number: 1234
Not a Spy Number