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

212 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");
        
    }
}

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

74 Likes