Cho số n, kiểm tra mọi chữ số của n có chia hết n không. Nếu số là 'xy' thì cả x và y phải chia hết n.
Input: 24 Output: Yes
Giải thích
24 % 2 == 0, 24 % 4 == 0
Duyệt từng chữ số, kiểm tra khác 0 và chia hết số gốc.
Ví dụ
#include <stdio.h>
int main(){
int n = 24;
int temp = n;
int flag=1;
while (temp > 0){
int r = n % 10;
if (!(r != 0 && n % r == 0)){
flag=0;
}
temp /= 10;
}
if (flag==1)
printf("The number is divisible by its digits");
else
printf("The number is not divisible by its digits");
return 0;
}
Kết quả
The number is divisible by its digits