【C语言】通过循环输出100∽500中符合“是7的倍数且不是3的倍数”的所有数

avatar

azurekiln

  1. 分别用3种循环编写程序,输出100∽500中符合“是7的倍数且不是3的倍数”的所有数,并统计符合上述条件的数字个数。
    
    #include<stdio.h>

int output_and_count(int i, int count) {
if (i % 7 == 0 && i % 3 != 0) {
printf("%d ", i);
(
count)++;
}
}

int main() {
int minNumber = 100, maxNumber = 500;
int i = minNumber, j = minNumber, k = minNumber;
int count_i = 0, count_j = 0, count_k = 0;
printf("在%d~%d中,是7的倍数且不是3的倍数的有:\n", minNumber, maxNumber);

printf("for 循环:\n");
// for 循环
for (i = minNumber; i <= maxNumber; i++) {
    output_and_count(i, &count_i);
}
printf("\n符合条件的数字个数:%d\n", count_i);

printf("\nwhile 循环:\n");
// while 循环
while (j <= maxNumber) {
    output_and_count(j, &count_j);
    j++;
}
printf("\n符合条件的数字个数:%d\n", count_j);

printf("\ndo...while 循环:\n");
// do...while 循环
do {
    output_and_count(k, &count_k);
    k++;
} while (k <= maxNumber);
printf("\n符合条件的数字个数:%d\n", count_k);

return 0;

}



![](../content/uploadfile/202510/a2941761556961.png)

扫描二维码,在手机上阅读
收藏
powered by emlog pro
服务器供应商 新区云数据

友情链接
白衣Ink


sitemap