(C 예제) 입력한 숫자의 각 자릿수를 더하여 3 또는 9로 나누어 떨어지는지 확인하는 예제 코드: do while(), if()

입력 숫자의 각 자릿수를 더하고 3 또는 9로 나누어 떨어지는지 확인하는 예제 코드: do while(), if()

포스트 난이도: HOO_Junior


#샘플 코드

#include <stdio.h>

int main() {
    int num, digit, sum=0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);

    do {
        digit = num % 10;
        sum += digit;
        num /= 10;
    } while (num != 0);

    printf("The sum of the digits = %d\n", sum);

    num = sum;
    do {
        num -= 3;
    } while (num >= 3);
    if (num == 0) {
        printf("%d is divisible by 3\n", sum);
    } else {
        printf("%d is not divisible by 3\n", sum);
    }

    num = sum;
    do {
        num -= 9;
    } while (num >= 9);
    if (num == 0) {
        printf("%d is divisible by 9\n", sum);
    } else {
        printf("%d is not divisible by 9\n", sum);
    }

    return 0;
}

# 결과

Enter a positive integer: 66
The sum of the digits = 12
12 is divisible by 3
12 is not divisible by 9

Enter a positive integer: 150
The sum of the digits = 6
6 is divisible by 3
6 is not divisible by 9