KnowledgeBoat Logo

Java Number Programs (ISC Classes 11 / 12)

Write a program to accept a number and check 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: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1*1*2*4 = 8

Java

Java Iterative Stmts

ICSE 2017

255 Likes

Answer

import java.util.Scanner;

public class KboatSpyNumber
{
    public void spyNumCheck() {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter Number: ");
        int num = in.nextInt();
        
        int digit, sum = 0;
        int orgNum = num;
        int prod = 1;
        
        while (num > 0) {
            digit = num % 10;
            
            sum += digit;
            prod *= digit;
            num /= 10;
        }
        
        if (sum == prod)
            System.out.println(orgNum + " is Spy Number");
        else
            System.out.println(orgNum + " is not Spy Number");
        
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to accept a number and check 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: Sample Input: 1124 Sum of the digits = 1 + 1 + 2 + 4 = 8 Product of the digits = 1*1*2*4 = 8

Answered By

85 Likes