用函数法求100-200内素数的和。C 语言代码
时间: 2024-03-06 19:50:46 浏览: 39
以下是使用函数法求100-200内素数的和的 C 语言代码:
```c
#include <stdio.h>
#include <stdbool.h>
int sumOfPrimes(int start, int end);
int main() {
int start = 100, end = 200;
int sum = sumOfPrimes(start, end);
printf("The sum of primes between %d and %d is %d\n", start, end, sum);
return 0;
}
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int sumOfPrimes(int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
```
该程序首先定义了一个 `isPrime` 函数,用于判断一个数是否为素数。然后定义了一个 `sumOfPrimes` 函数,用于计算给定区间内素数的和。最后在主函数中调用 `sumOfPrimes` 函数,计算100-200内素数的和并输出结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)