我无法弄清楚为什么我打印出给定范围内所有素数的特定代码没有提供所有素数

分享于2022年07月17日 c++ cin for-loop loops nested 问答
【问题标题】:我无法弄清楚为什么我打印出给定范围内所有素数的特定代码没有提供所有素数(I am not able to figure why my particular code to print out all prime numbers within a given range is not providing all the prime numbers)
【发布时间】:2022-07-08 22:45:17
【问题描述】:

我无法弄清楚为什么我打印出给定范围内所有素数的特定代码没有提供所有素数

#include
using namespace std;
int main()
{
    int n{};
    int count{};
    cout<<"Enter the range ";
    cin>>n;
    for(int i=2;i


【解决方案1】:

你必须计算每个数字的除数,而不是所有数字的总和。

    for(int i=2;i