Please program to output all integers between 0 and 200 that are divisible by 3 and whose single digit is 6.
#include <stdio.h>
int hasSinglDigit(int x, int d) {
int has=0;
if (x == 0 && d == 0)
return 1;
while (x>0) {
if (x%10 == d) {
if (has) {
return 0;
}
has = 1;
}
x /= 10;
}
return has;
}
int main() {
int n=0;
for (int i=0; i<=200; i++) {
if (i%3 == 0 && hasSinglDigit(i, 6)) {
if (n == 0) {
printf("%d", i);
}
else {
printf(", %d", i);
}
n++;
}
}
printf("\n");
return 0;
}
Comments
Leave a comment