Spy Number in JavaIn this section, we will learn what is a spy number and also create Java programs to check if the given number is Spy or not. The spy number program is frequently asked in Java coding test. Spy NumberA positive integer is called a spy number if the sum and product of its digits are equal. In other words, a number whose sum and product of all digits are equal is called a spy number. Example of Spy NumberLet's take the number 1124 and check whether the number is a spy or not. First, we will split it into digits (1, 1, 2, 4). After that find the sum and product of all the digits. Sum=1+1+2+4=8 Product=1*1*2*4=8 We observe that the sum and product of the digits both are equal. Hence, 1124 is a spy number. Similarly, we can check other numbers also. Some other spy numbers are 22, 123, 132, etc. Steps to Find Spy Number
Let's implement the above steps in a Java program. Spy Number Java ProgramSpyNumberExample1.java Output 1: Enter the number to check: 123 The given number is a spy number. Output 2: Enter the number to check: 456 The given number is a not spy number. SpyNumberExample2.java Output: Enter the lower range: 1 Enter upper range: 10000 The Spy numbers between 1 to 10000 are: 1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421 2114 2141 2411 4112 4121 4211
Next TopicTypes of Applets in Java
|