C语言找出范围内最大的素数
时间: 2023-10-07 12:07:05 浏览: 162
以下是一个可以找出指定范围内最大素数的C程序:
```c
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int start, end, max_prime = -1;
printf("Enter the range to find the largest prime number:\n");
printf("Start: ");
scanf("%d", &start);
printf("End: ");
scanf("%d", &end);
for (int i = start; i <= end; i++) {
if (is_prime(i)) {
max_prime = i;
}
}
if (max_prime == -1) {
printf("There is no prime number in the range.\n");
} else {
printf("The largest prime number in the range is %d.\n", max_prime);
}
return 0;
}
```
该程序中,`is_prime`函数用于判断一个数是否为素数。在`main`函数中,用户输入范围,并使用循环遍历范围内的每个数,判断是否为素数,并记录最大素数。最后输出结果。
相关推荐
![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)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)