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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More