C语言程序:统计数字2的出现次数
#include <stdio.h>
int main() {
int a = 2, b = 22, c = 222, d = 2222;
int count = 0;
// 计算a中2的个数
while (a > 0) {
if (a % 10 == 2)
count++;
a /= 10;
}
// 计算b中2的个数
while (b > 0) {
if (b % 10 == 2)
count++;
b /= 10;
}
// 计算c中2的个数
while (c > 0) {
if (c % 10 == 2)
count++;
c /= 10;
}
// 计算d中2的个数
while (d > 0) {
if (d % 10 == 2)
count++;
d /= 10;
}
printf("总个数:%d\n", count);
return 0;
}
输出结果为:总个数:7
原文地址: https://www.cveoy.top/t/topic/bzQp 著作权归作者所有。请勿转载和采集!