编写函数,求出在正整数m和n之间(m<n)能被3整除、但不能被5整除的数的个数,在主函数中输入正整数m,n(m<n),调用该函数,输出函数返回结果。用c语言编写
时间: 2024-05-03 21:19:52 浏览: 116
以下是 c 语言代码实现:
```
#include <stdio.h>
int count(int m, int n) {
int cnt = 0;
for (int i = m; i <= n; i++) {
if (i % 3 == 0 && i % 5 != 0) {
cnt++;
}
}
return cnt;
}
int main() {
int m, n;
printf("请输入正整数m和n(m<n): ");
scanf("%d %d", &m, &n);
printf("在%d和%d之间能被3整除、但不能被5整除的数的个数为%d\n", m, n, count(m, n));
return 0;
}
```
注意事项:
1. 在 `count` 函数中,我们通过遍历区间 `[m, n]` 中的每个数,判断该数能否被 3 整除且不能被 5 整除。
2. 对于满足条件的数,我们累计其个数,最后返回计数器的值。
3. 在 `main` 函数中,我们先读入区间的左右端点,然后调用 `count` 函数求解,最后输出结果。
希望这个例子能帮助您理解如何在 c 语言中编写函数。
相关问题
编写函数,求出在正整数m和n之间(m<n)能被3整除、但不能被5整除的数的个数,在主函数中输入正整数m,n(m<n),调用该函数,输出函数返回结果。
```python
def count_num(m, n):
count = 0
for i in range(m+1, n):
if i % 3 == 0 and i % 5 != 0:
count += 1
return count
m = int(input("请输入正整数m:"))
n = int(input("请输入正整数n:"))
print("在%d和%d之间能被3整除、但不能被5整除的数的个数为:%d" % (m, n, count_num(m, n)))
```
C语言编写函数,求出在正整数m和n之间(m<n)能被3整除、但不能被5整除的数的个数,在主函数中输入正整数m,n(m<n),调用该函数,输出函数返回结果。
```c
#include <stdio.h>
int count(int m, int n) {
int cnt = 0;
for (int i = m; i <= n; i++) {
if (i % 3 == 0 && i % 5 != 0) {
cnt++;
}
}
return cnt;
}
int main() {
int m, n;
printf("请输入正整数m和n,中间用空格隔开:");
scanf("%d %d", &m, &n);
printf("在%d和%d之间能被3整除、但不能被5整除的数的个数为%d\n", m, n, count(m, n));
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)