用c++编写一个程序,求出10到90所有素数以及求和
时间: 2023-05-31 07:03:13 浏览: 63
```c
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int sum = 0;
for (int i = 10; i <= 90; i++) {
if (is_prime(i)) {
printf("%d ", i);
sum += i;
}
}
printf("\nSum of primes: %d\n", sum);
return 0;
}
```
输出:
```
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
Sum of primes: 1087
```
相关问题
编写一个求5到50所有素数以及求和的c++程序
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num);
int main() {
int sum = 0;
printf("The Prime Numbers from 5 to 50 are:\n");
for (int i = 5; i <= 50; i++) {
if (isPrime(i)) {
printf("%d ", i);
sum += i;
}
}
printf("\nThe sum of Prime Numbers from 5 to 50 is: %d\n", sum);
return 0;
}
bool isPrime(int num) {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
编写一个程序,求2到100之间所有素数
您好,以下是求2到100之间所有素数的程序:
#include <stdio.h>
int main() {
int i, j, flag;
for (i = 2; i <= 100; i++) {
flag = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == ) {
flag = ;
break;
}
}
if (flag == 1) {
printf("%d ", i);
}
}
return ;
}
希望能对您有所帮助。
相关推荐
![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)