In the previous article, we have discussed Java Program to Check Special Number
In this article we are going to understand what Spy number is and how we can check whether a number is Spy or not in Java with examples.
Program to Check Spy Number
Spy numbers are numbers whose sum of digits is equal to the product of the digits.
Example: Number=22: 2+2 = 4; 2*2 = 4 Spy number Number=19: 1+9=10; 1*9 = 9 Not a Spy number Number=4: 4 Spy number
In the above examples the numbers 22 and 4 are Spy numbers. But 19 is not a spy number as the sum and product of its digits are not equal.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach :
- We ask the user to enter a number and store it .
- We calculate the sum and product of its digits.
- If the resultant sum is the same as resultant product, then the number is said to be a Spy number.
Let’s see different ways to check Spy number.
Method-1: Java Program to Check Spy Number By Static Value
import java.util.Scanner; public class SpyNumber { public static void main(String args[]) { //Number declared int num = 22; int temp = num,prod = 1, sum =0, remainder; //Loop to iterate through the digits and calculate um and prod while(temp>0) { remainder = temp%10; sum += remainder; prod *= remainder; temp = temp /10; } if(sum==prod) { System.out.println(num+" is a spy number"); } else { System.out.println(num+" is not a spy number"); } } }
Output: 22 is a spy number
Method-2: Java Program to Check Spy Number By User Input Value
import java.util.Scanner; public class SpyNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int temp = num,prod = 1, sum =0, remainder; //Loop to iterate through the digits and calculate um and prod while(temp>0) { remainder = temp%10; sum += remainder; prod *= remainder; temp = temp /10; } if(sum==prod) { System.out.println(num+" is a spy number"); } else { System.out.println(num+" is not a spy number"); } } }
Output: Case-1 Enter a number : 22 22 is a spy number Case-2 Enter a number : 45 45 is not a spy number
Method-3: Java Program to Check Spy Number By Using User Defined Method
import java.util.Scanner; public class SpyNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //calling the user defined method //to check spy number or not. checkNumber(num); } //checkNumber() method to check spy number public static void checkNumber(int num) { int temp = num,prod = 1, sum =0, remainder; //Loop to iterate through the digits and calculate um and prod while(temp>0) { remainder = temp%10; sum += remainder; prod *= remainder; temp = temp /10; } if(sum==prod) { System.out.println(num+" is a spy number"); } else { System.out.println(num+" is not a spy number"); } } }
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding
Related Java Programs: