C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs

A number is a Spy number, if sum and product of all digits are equal. In this java program, we are going to check whether a given number is SPY number or not? Submitted by IncludeHelp, on October 29, 2017

Given a number, and we have to check whether it is Spy number or not using Java program.

A number is a Spy number, if sum and product of all digits are equal.

Example:

Number 123 is a Spy number, sum of its digits is 6 (1+2+3 =6) and product of its digits is 6 (1x2x3 = 6), sum and product are same, thus, 123 is a spy number.

import java.util.Scanner; public class SpyNumber { public static void main(String[] args) { int n,product=1,sum=0; int ld; // create object of scanner. Scanner sc = new Scanner(System.in); // you have to enter number here. System.out.print("Enter the number :" ); // read entered number and store it in "n". n=sc.nextInt(); // calculate sum and product of the number here. while(n>0) { ld=n%10; sum=sum+ld; product=product*ld; n=n/10; } // compare the sum and product. if(sum==product) System.out.println("Given number is spy number"); else System.out.println("Given number is not spy number"); } }

Output

First run: Enter the number :1124 Given number is spy number Second run: Enter the number :1123 Given number is not spy number

Liked this article? Do share with your friends :)

C, C++, Java, D.S., Python, .Net, SQL, PL/SQL, Ajax, PHP, JavaScript, CSS, HTML, C programs, C++ programs, Java programs, C# programs, DS programs, C aptitude, C++ aptitude, Java aptitude, DBMS aptitude, O.S., Networking, Embedded systems, Nanotechnologies, Linux, DOS, puzzles, syntaxes,