求1到n中能被3或7整除的数之和 C语言代码
时间: 2023-05-22 12:02:18 浏览: 134
以下是求1到n中能被3或7整除的数之和的C语言代码:
```c
#include <stdio.h>
int main() {
int n, sum = 0;
printf("请输入n的值:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 7 == 0) {
sum += i;
}
}
printf("1到%d中能被3或7整除的数之和为:%d\n", n, sum);
return 0;
}
```
希望能对您有所帮助!
相关问题
求1到n中能被3或7整除的数之和 C语言代码 while循环语句
以下是求1到n中能被3或7整除的数之和的C语言代码,使用while循环语句:
```c
#include <stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("请输入n的值:");
scanf("%d", &n);
while (i <= n) {
if (i % 3 == 0 || i % 7 == 0) {
sum += i;
}
i++;
}
printf("1到%d中能被3或7整除的数之和为:%d\n", n, sum);
return 0;
}
```
求1到100中能被3或7整除的数之和 C语言代码
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 || i % 7 == 0) {
sum += i;
}
}
printf("1到100中能被3或7整除的数之和为:%d", sum);
return 0;
}
阅读全文
相关推荐














