Digit Check in C


/ Published in: C
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. bool testProduct(int number, int numDigits, int[] digits) {
  2. int x = 0;
  3. int i = 0;
  4. bool found = false;
  5. while (number > 0) {
  6. i = number % 10;
  7. number /= 10;
  8. for (x = 0; x < numDigits; x++) {
  9. if (digits[x] == i) {
  10. found = true;
  11. break;
  12. }
  13. }
  14. if (!found) return false;
  15. found = false;
  16. }
  17. return true;
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.