#include <stdio.h>
#include <stdbool.h>
// 函數(shù)聲明
bool isPrime(int num);
int main() {
? ? int sum = 0;
? ? printf("1到100之間的質(zhì)數(shù)有:\n");
? ? for (int i = 2; i <= 100; ++i) {
? ? ? ? if (isPrime(i)) {
? ? ? ? ? ? printf("%d ", i);
? ? ? ? ? ? sum += i;
? ? ? ? }
? ? }
? ? printf("\n質(zhì)數(shù)的和為:%d\n", sum);
? ? return 0;
}
// 判斷一個數(shù)是否為質(zhì)數(shù)的函數(shù)
bool isPrime(int num) {
? ? if (num < 2) {
? ? ? ? return false;
? ? }
? ? for (int i = 2; i * i <= num; ++i) {
? ? ? ? if (num % i == 0) {
? ? ? ? ? ? return false; // 不是質(zhì)數(shù)
? ? ? ? }
? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-828814.html
? ? return true; // 是質(zhì)數(shù)
}文章來源地址http://www.zghlxwxcb.cn/news/detail-828814.html
到了這里,關(guān)于【水文】計算并輸出1到100之間所有質(zhì)數(shù)的和的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!