SPY Number in JAVA

SPY Number in JAVA


A Number is said to be a SPY Number if the sum and product of all digits are equal.

Example: Number 123 is a Spy number, sum of its digits is 6

Algorithm and Program below :-

ALGORITHM :-
  1. Get the number to check for Spy Number
  2. Hold the number in temporary variable
  3. Store the sum of its digits in a variable
  4. Store the product of its digits in another variable
  5. Compare the sum and product digits of the number
  6. If both are equal print "Spy Number"
  7. If not print "Not a Spy Number"

PROGRAM below :-

import java.io.*;
import java.util.*;
class Spy
{
    public static void main(String args[])
    {
        Scanner in= new Scanner(System.in);
        int n,p,rev,s=0,sum=1;
        System.out.println("Enter No.");
        n= in.nextInt(); // Input number from user
        p=n; // store the entered number in "p" variable
        while(n>0)
        {
            rev=n%10; // it extract last digit of the number
            s=s+rev; // store the sum of digits
            sum=sum*rev; // store product of digits
            n=n/10; // it extract all digit except the last
        }
        if(sum==s) // comparing with product of digits with sum of digits
        {
            System.out.println("It is Spy Number : "+p);
        }
        else
        {
            System.out.println("It is not Spy Number : "+p);
        }
    } // end of main method
} // end of class



Follow me on Instagram

All the Best :)
Keep Learning :) 

Comments

Post a Comment

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

PalPrime Number in JAVA