gpt4 book ai didi

c - 如何计算输入文件中数字的倍数?

转载 作者:行者123 更新时间:2023-11-30 14:30:44 25 4
gpt4 key购买 nike

我试图从用户输入文件中分别计算 2、3 和 6 的倍数数量。但由于某种原因,我的计数器不工作。任何博西可以帮助我吗?我的代码:

#include <stdio.h>
int main (void)
{
int num[12];
int i;
int counttwo;
int countthree;
int countsix;
int total=0;
printf("enter 12 integer numbers:\n");
for(i=0;i<12;i++){

scanf("%d", &num[i]);
}
for(i=0;i<12;i++){
counttwo=0;
if(num[i]%2==0){
counttwo++;
}
countthree=0;
if(num[i]%3==0)
{
countthree++;
}
countsix=0;
if(num[i]%6==0)
{
countsix++;
}
printf("There are %d multiples of 2:\n", counttwo);
printf("There are %d multiples of 3:\n", countthree);
printf("There are %d multiples of 6:\n", countsix);
}
return 0;

}

最佳答案

您在每个迭代步骤中重置计数器变量。把

counttwo=0;
countthree=0;
countsix=0;

for()之前的代码。

关于c - 如何计算输入文件中数字的倍数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2754529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com