Programming Examples

Java program to accept a number and check and display whether it is a spy number or not


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 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

Solution

import java.util.*;
class SpyNumber
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int number,digit,product=1,sum=0;
        System.out.println("Enter any Integer number:");
        number=sc.nextInt();
        while(number>0)
        {
            digit=number%10;
            sum=sum+digit;
            product=product*digit;
            number=number/10;
        }
        if(sum==product)
        {
            System.out.println("It is Spy Number");
        }
        else
        {
            System.out.println("It is not a Spy Number");
        }
    }
}
Output

Enter any Integer number:

1124

It is Spy Number

-----------------------------------------
Enter any Integer number:
234
It is not a Spy Number
CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz