Return to Snippet

Revision: 47053
at May 31, 2011 10:43 by rtperson


Initial Code
bool testProduct(int number, int numDigits, int[] digits) {
    int x = 0;
    int i = 0;
    bool found = false;
    while (number > 0) {
        i = number % 10;
        number /= 10;
        for (x = 0; x < numDigits; x++) {
            if (digits[x] == i) {
                found = true;
                break;
            }
        }
        if (!found) return false;
        found = false;
    }
    return true;
}

Initial URL

                                

Initial Description
Check that a given integer consists only of elements of a given set. It does a linear search through the set, so the assumption is that the set to search is relatively small. If you're chewing through a particularly large set of digits, you may want to sort your set and implement a binary search.

Initial Title
Digit Check in C

Initial Tags
c

Initial Language
C