top of page
Writer's pictureSatyam Gupta

SPY Number in JAVA

SPY Number


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

Input : 123

Sum of digits : 1+2+3 = 6

Product of digits : 1 x 2 x 3 = 6

Output : It is a Spy Number


 

Algorithm and Program below :-


Algorithm :-

  • Get the number to check for Spy Number

  • Hold the number in temporary variable

  • Store the sum of its digits in a variable

  • Store the product of its digits in another variable

  • Compare the sum and product digits of the number

  • If both are equal print "Spy Number"

  • If not print "Not a Spy Number"

 

Program :-

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

 

Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.


All the Best :)

Keep Learning :)


51 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page