用c语言 求[m,n]之间所有不能被3整除的整数之和,m,n的值由键盘输入。 输入样例:3,12 输出样例:s=45
时间: 2024-03-01 14:52:41 浏览: 60
以下是 C 语言代码实现:
```c
#include <stdio.h>
int main() {
int m, n;
scanf("%d,%d", &m, &n);
int s = 0;
for (int i = m; i <= n; i++) {
if (i % 3 != 0) {
s += i;
}
}
printf("s=%d\n", s);
return 0;
}
```
输入格式和输出格式与题目描述一致。
相关问题
C语言求[1,n]之间所有不能被3整除的整数之和,n的值由键盘输
您好!可以使用以下的C语言代码来求[1,n]之间所有不能被3整除的整数之和,其中n的值由键盘输入:
```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) {
sum += i;
}
}
printf("[1, %d]之间所有不能被3整除的整数之和为:%d\n", n, sum);
return 0;
}
```
在代码中,我们使用了for循环来遍历[1,n]之间所有的整数,如果该整数不能被3整除,则将该整数累加到sum中。
希望能够帮到您!
用C语言求[1,n]之间所有不能被3整除的整数之和,n的值由键盘输入,用函数方法解决
好的,以下是使用函数的方法来解决该问题的C语言代码:
```c
#include <stdio.h>
int sum(int n) { // 定义一个函数sum,用于求[1, n]之间所有不能被3整除的整数之和
int s = 0; // 定义一个变量s,用于存储所有满足条件的数值之和
for (int i = 1; i <= n; i++) {
if (i % 3 != 0) {
s += i;
}
}
return s; // 返回所有满足条件的数值之和
}
int main() {
int n;
printf("请输入n的值:");
scanf("%d", &n);
printf("[1, %d]之间所有不能被3整除的整数之和为:%d\n", n, sum(n)); // 调用函数sum,输出结果
return 0;
}
```
在这个代码中,我们定义了一个名为sum的函数,该函数接收一个整数n作为参数,返回[1,n]之间所有不能被3整除的整数之和。然后在主函数中,我们通过调用函数sum来输出结果。
希望能够帮到您!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)