Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Chương trình C kiểm tra tất cả chữ số của một số có chia hết nó không


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