A number is known as a spy number if the sum of its digits is exactly equal to the product of its digits.

```
#include <iostream>
using namespace std;
bool checkSpy(int number) {
int sum = 0, mul = 1, rem;
while (number > 0) {
rem = number % 10;
sum += rem;
mul *= rem;
number = number / 10;
}
if (sum == mul) {
return true;
}
else {
return false;
}
}
int main() {
int number;
cout << "Enter a number to check for spy number: ";
cin >> number;
if (checkSpy(number)) {
cout << number << " is a spy number";
} else {
cout << number << " is not a spy number";
}
return 0;
}
```

```
Enter a number to check for spy number: 123
123 is a spy number
```

Here’s an explanation of the code:

```
#include <iostream>
using namespace std;
```

The first two lines include the `iostream`

library and the `std`

namespace, which provides access to the standard input and output streams (`cin`

and `cout`

) used in the code.

```
bool checkSpy(int number) {
int sum = 0, mul = 1, rem;
```

This is the definition of the function `checkSpy`

. It takes an integer argument `number`

, which is the number being tested to see if it’s a spy number. Inside the function, several variables are declared:

`sum`

: the sum of the digits of`number`

, initialized to 0`mul`

: the product of the digits of`number`

, initialized to 1`rem`

: the remainder when dividing`number`

by 10, used to extract the individual digits of`number`

```
while (number > 0) {
rem = number % 10;
sum += rem;
mul *= rem;
number = number / 10;
}
```

This while loop continues until all the digits of `number`

have been processed. Inside the loop, the remainder when `number`

is divided by 10 is extracted and stored in `rem`

. This gives the rightmost digit of `number`

. The value of `sum`

is then incremented by `rem`

, and the value of `mul`

is multiplied by `rem`

. Finally, `number`

is divided by 10, discarding the rightmost digit and moving on to the next digit.

```
if (sum == mul) {
return true;
}
else {
return false;
}
}
```

At the end of the loop, if the sum of the digits is equal to their product, then the function returns `true`

, indicating that `number`

is a spy number. Otherwise, it returns `false`

, indicating that `number`

is not a spy number.

```
int main() {
int number;
cout << "Enter a number to check for spy number: ";
cin >> number;
if (checkSpy(number)) {
cout << number << " is a spy number";
} else {
cout << number << " is not a spy number";
}
return 0;
}
```

The `main`

function is the entry point of the program. It first prompts the user to enter a number to be tested, then calls the `checkSpy`

function with the user’s input as an argument. The result of the function is used to determine whether the number is a spy number or not, and the result is output to the screen. The `return 0;`

statement indicates that the program has executed successfully.

The code is a C++ program that finds “spy numbers” within a given upper bound. A spy number is a number whose sum of digits is equal to the product of its digits. The code has two functions: `checkSpy`

and `main`

.

The `checkSpy`

function takes an integer `number`

as input and returns a `bool`

indicating whether or not the number is a spy number. The function starts by initializing two variables, `sum`

and `mul`

, to 0 and 1, respectively. These variables will be used to keep track of the sum and product of the digits of the number.

Next, the function uses a `while`

loop to iterate over each digit of the number. It uses the modulo operator (`%`

) to extract the last digit, adds it to `sum`

, multiplies it by `mul`

, and then divides the number by 10 to shift the decimal place to the right. This process is repeated until the number is no longer greater than 0.

After the loop, the function checks if `sum`

is equal to `mul`

. If so, it returns `true`

indicating that the number is a spy number. If not, it returns `false`

.

The `main`

function is the entry point of the program. It starts by prompting the user to enter an upper bound and storing it in a variable `max`

. It then uses a `for`

loop to iterate from 1 to `max`

and calls the `checkSpy`

function for each number. If the function returns `true`

, the number is printed as a spy number.

Finally, the function returns 0 to indicate a successful execution.

```
#include <iostream>
using namespace std;
bool checkSpy(int number) {
int sum = 0, mul = 1, rem;
while (number > 0) {
rem = number % 10;
sum += rem;
mul *= rem;
number = number / 10;
}
if (sum == mul) {
return true;
}
else {
return false;
}
}
int main() {
int max;
cout << "Enter the upper bound to find spy numbers: ";
cin >> max;
cout << "Spy numbers:" << endl;
for (int i = 1; i <= max; i++) {
if (checkSpy(i)) {
cout << i << " ";
}
}
return 0;
}
```

```
Enter the upper bound to find spy numbers: 1500
Spy numbers:
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421
```

Login

Accessing this tutorial requires a login. Please enter your credentials below!