Write A Program To Check Entered Number Is A Spy Number Or Not Through Java Program

Share Your Love

In this post I am going to discuss a program called, Entered Number A Spy Number Or Not Through Java Program.

What is a Spy Number?

When you enter a natural number like 1124 then sum up its digit is equal to the product of all the digits. Like,

//Example:
Suppose n=1124
Product of digits on prod=(1 * 1 * 2 * 4) = 8
Sum of digits on sum=(1 + 1 + 2 + 4) = 8
Here, sum==prod both product and sum are equal.
Output: Spy Number

Input : 1412
Explanation : 
sum = (1 + 4 + 1 + 2) = 8
product = (1 * 4 * 1 * 2) = 8
since, sum == product == 8
Output : Spy Number

Input : 132
Explanation : 
sum = (1 + 3 + 2) = 6
product = (1 * 3 * 2) = 6
since, sum == product == 6
Output : Spy Number

C program to check number is a spy number or not:

// Java program to
// check Spy number
import java.util.*;

class GFG
{
	
	// boolean function to
	// check Spy number
	static boolean checkSpy(int input)
	{

		int digit, sum = 0,
			product = 1;
		while (input > 0)
		{
			digit = input % 10;
			
			// getting the
			// sum of digits
			sum += digit;
			
			// getting the product
			// of digits
			product *= digit;
			input = input / 10;
		}
		
		// Comparing the
		// sum and product
		if (sum == product)
			return true;
		else
			return false;
	}
	
	// Driver code
	public static void main(String args[])
	{
		int input = 1412;
		if (checkSpy(input))
			System.out.println("The number is "+
								"a Spy number");
		else
			System.out.println("The number is "+
							"NOT a Spy number");
	}
}
Case-1:
The number is a Spy number

If you find this post helpful please comment and share this post and wants to improve WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter