java program for spy number

java program for spy number

Spy Number program

Write a program to accept a number and check and display whether it is a Spy Number or not.
A Number is spy number 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
 Product of the digits = 1 * 1 * 2 * 4




// ICSE 2017 Question Answer

import java.util.*;
public class SpyNumber
{
    public static void main(String args[])
    {
        int no, pro, sum, digit;
        pro = 1;
        sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number to check spy number :");
        no = sc.nextInt();
        while(no != 0)
        {
            digit = no % 10;
            pro = pro * digit;
            sum = sum + digit;
            no = no / 10;
        }
        if(sum == pro)
        {
            System.out.println("Spy Number");
        }
        else
        {
            System.out.println("Not Spy Number");
        }
    }
}



Output:
Enter a number to check spy number:
1124
Spy Number
Enter a number to check spy number:
1254
Not Spy Number

Variable Description

Sr. No
Variable / Function
Type
Description
1
no
int
Taking number for spy number checking
2
pro
int
For product of digits
3
sum
int
For summation of digits
4
digit
int
Digit to add pr multiply
5
sc
Object
Scanner class object

More Java Program

circular prime number program in java

SHARE THIS
Previous Post
Next Post