Spy Number in Java2 min read

In this tutorial, we will write java programs to check for Spy Numbers. We will look at two different java programs son Spy Number

  1. Java Program to Check If a Number is Spy number or not.
  2. Java Program to print all the Spy Number within a given Range.

What is Spy Number?

A number is said to be a spy number if the sum of all its digits is equal to the product of all its digits.
Example:

Number: 123 
Sum of its digits: 1+2+3 = 6
Product of its digits: 1*2*3 = 6  
Sum and Product are equal, hence, 123 is a Spy number.

Similarly, you may check for other numbers such as 22, 132, 1124, etc.


Spy Number in Java Program

1. Java Program to Check If a Number is Spy number or not

Output:

//First Run
Enter the number: 1124
1124 is a spy number.

//Second Run
Enter the number: 166
166 not a spy number.


2. Java Program to print all the Spy Number within a given Range.

We will create a separate Boolean function (isSpyNumber()) that will return true if the sum of the digits and the product of the digits are equal else will return false and print them accordingly.

The program takes the input for the lower range and upper range value and will pass those numbers between this range to the function as an argument.

Output:

Enter the Lower Range: 1
Enter the Upper Range: 1000
The Spy numbers between 1 and 1000 are:
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …