Determine if a number contains a digit for class assignment

14,429

Breaking the problem down is the key do not jump to code, start off by asking yourself, how do I extract digits?

Use the % operator with 10. That's your current number % 10. Why 10? and not any other number? - We need to get the remainder after division which is what the modulo operator does any other number doesn't cut it, try it on a calculator.

So far so good, now what else do you need to do? You need to move forward in your search and compare the remaining digits. 147 % 10 already gave you 7 and you want to look at 14, to separate 14 from 7 you divide by 10 and get the remaining part not including 7. You continue your scan till you either find the number or are out of numbers which is the result. There is a problem here, a follow up, does your code work for negative numbers? I will leave that up to you to figure out.

We are left with the following code,

bool containsDigit(int number, int digit)
{
    while (number != 0)
    {
        int curr_digit = number % 10;
        if (curr_digit == digit) return true;
        number /= 10;
    }

    return false;
}
Share:
14,429
coco
Author by

coco

Updated on June 14, 2022

Comments

  • coco
    coco almost 2 years

    Write a function named containsDigit that determines if a number contains a particular digit.

    The header should look like:

    bool containsDigit(int number, int digit);
    

    If number contains digit, then the function should return true. Otherwise, the function should return false.

    Input: 
    147 9
    
    Output: 
    false
    

    I don't know why I always get false when I write like this:

    bool containsDigit(int number, int digit);
    
    int main() {
      double con;
      int number, digit;
      cout << "Input a number and a digit:\n";
      cin >> number >> digit;
      con = containsDigit(number, digit);
      cout << con;
      return 0;
    }
    
    bool containsDigit(int number, int digit) {
      int a(0), b;
      b = number;
      while (number > 0) {
        a = a + 1;
        number = number / 10;
      }
      cout << a;
      while (a > 1) {
        a = a - 1;
    
        if (b / pow(10, a) == digit) {
          cout << "true\n";
          break;
        } else {
          if (a == 1)
            cout << "false\n";
          else
            cout << "";
        }
        b = b % pow(10, a);
      }
    }
    
    • Dúthomhas
      Dúthomhas over 6 years
      Do you remember how to split a number into individual digits with a loop? Do that. Each time you get a digit, check if it matches the one you are looking for. If you get to the end of the loop, then no, the number does not contain the digit.
    • coco
      coco over 6 years
      bool containsDigit(int number, int digit) { int a(0),b; b=number; while(number>0) { a=a+1; number=number/10; } cout<<a; while(a>1) { a=a-1; if(b/pow(10,a)==digit) {cout<<"true\n"; break;} else { if(a==1) cout<<"false\n"; else cout<<""; } b=b-(b/pow(10,a))*pow(10,a); } } I don't know why I always get false when I write like this.
    • Disillusioned
      Disillusioned over 6 years
      You defined your function to return bool. Why do you assign the result to double?
    • coco
      coco over 6 years
      because the question want me to define true or false
    • Disillusioned
      Disillusioned over 6 years
      @coco I didn't ask why you defined it bool; that's the obvious thing to do. I asked why do you assign the result to a double con;? Also you should note that your function doesn't return anything.
  • Disillusioned
    Disillusioned over 6 years
    Although while (number) works, it relies on technicalities not immediately obvious to a beginner. I suggest while (number != 0) would be more explicit.
  • scopchanov
    scopchanov over 6 years
    @CraigYoung, Actually, it is better for such cases to use a pseudo language or some kind of a standard diagram to represent the behavior. That way the so called technicalities are avoided and it is up to the implementer to do it in his/her own style. It has a better educational effect too.