How to check if specific digit exists in a number with C++

1 Answer

0 votes
#include <iostream>

bool check_digit_exists(int n, int digit) {
    while (n > 0) {
        if (digit == n % 10) {
            return true;
        }
        n = n / 10;
    }
    return false;
}
  
int main(void)
{
    int num = 230138;
     
    std::cout << check_digit_exists(num, 8) << "\n";
     
    std::cout << check_digit_exists(num, 5);
}
  
    
    
    
/*
run:
    
1
0
 
*/

 



answered Oct 3, 2023 by avibootz

Related questions

1 answer 128 views
2 answers 153 views
1 answer 123 views
2 answers 204 views
2 answers 155 views
...