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

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>
 
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;
    
    printf("%d\n", check_digit_exists(num, 8));
    
    printf("%d\n", check_digit_exists(num, 5));
    
    return 0;
}
 
   
   
   
/*
run:
   
1
0

*/

 



answered Oct 3, 2023 by avibootz

Related questions

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