Programming Languages/C and C++
[C Examples] 입력된 숫자의 각 자리수를 더하고 3과 9로 나누어지는지 살펴보는 예제코드: do while(), if()
Henry Cho
2023. 3. 23. 08:49
입력된 숫자의 각 자리수를 더하고 3과 9로 나누어지는지 살펴보는 예제코드: do while(), if()
포스트 난이도: HOO_Junior
# Example Codes
#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;
}
# Results
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
728x90