求m~n之间不能被3整除的数之和
时间: 2024-02-13 13:06:37 浏览: 32
好的,根据您的要求,我可以为您编写以下代码来求解该问题:
```python
m = int(input("请输入m的值:"))
n = int(input("请输入n的值:"))
sum = 0
for i in range(m, n+1):
if i % 3 != 0:
sum += i
print("m~n之间不能被3整除的数之和为:", sum)
```
您只需要输入m和n的值,程序就会计算出m~n之间不能被3整除的数之和并输出。希望这个程序能够解决您的问题。
相关问题
用C语言函数写出m~n之间不能被3整除的数之和
好的,根据您的要求,我可以为您编写以下C语言函数来求解该问题:
```c
#include <stdio.h>
int sum(int m, int n) {
int i, s = 0;
for (i = m; i <= n; i++) {
if (i % 3 != 0) {
s += i;
}
}
return s;
}
int main() {
int m, n;
printf("请输入m的值:");
scanf("%d", &m);
printf("请输入n的值:");
scanf("%d", &n);
printf("m~n之间不能被3整除的数之和为:%d\n", sum(m, n));
return 0;
}
```
您只需要调用`sum`函数并传入m和n的值,程序就会计算出m~n之间不能被3整除的数之和并输出。希望这个函数能够解决您的问题。
用C语言函数求出m~n不能被3整除的数之和
可以使用以下的C语言函数来求解m~n之间不能被3整除的数之和:
```c
#include <stdio.h>
int sum(int m, int n) {
int i, s = 0;
for (i = m; i <= n; i++) {
if (i % 3 != 0) {
s += i;
}
}
return s;
}
int main() {
int m, n;
printf("请输入m和n的值:\n");
scanf("%d%d", &m, &n);
printf("%d到%d之间不能被3整除的数之和为:%d\n", m, n, sum(m, n));
return 0;
}
```
这里使用了一个for循环来遍历m~n之间的所有数,如果当前的数不能被3整除,则累加到总和s中。最后将总和s返回。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)