Spy number

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

Check Spy Number

#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;
}
Output
Enter a number to check for spy number: 123
123 is a spy number

Spy Number List (1-n)

#include &lt;iostream&gt;
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;
}
Output
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