Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

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

  1. Spy Number Definition: A number where
  2. Sum of digits = Product of digits
  3. Extract digits using % 10
  4. Add to sum and multiply to product
  5. Compare sum and product to determine if it’s a Spy Number