是什么导致我的质数全部消失? [C ++]

时间:2018-07-26 08:31:28

标签: c++

我已经制定了一种算法,该算法在理论上应该显示人类可以手动输入的范围内的每个数字。它的大脑是这样编程的。

//Prime Number Displayer
#include <iostream>
using namespace std;

int main(){
int Low, High, check;


cout<<"Enter positive integer."<<endl;
cin>>Low;

cout<<"Enter a second positive integer."<<endl;
cin>>High;

cout<<"You will get a set of all prime numbers between "<<Low<<" and "<<High<<"."<<endl;

check=(Low%2);

    while (Low <=High){

if(check==1){

cout<<""<<Low<<""<<endl;

check++;
}
}




    return 0;
}

我希望程序在一行上显示范围内的每个素数。除了输出的低范围上的数字外,我似乎没有其他任何结果。请,谢谢。

1 个答案:

答案 0 :(得分:-3)

这样的代码将在用户指定的范围内找到素数:

bool isPrime(int n)
{
    if (n <= 1)
        return false;

    if (n%2 == 0 && n != 2)
        return false;

    for (int i = 3; i < n; i+=2)
        if (n % i == 0)
            return false;

    return true;
}


int main()
{
    int Low, High;
    bool isPrimeNum;

    cout<<"Enter positive integer."<<endl;
    cin>>Low;

    cout<<"Enter a second positive integer."<<endl;
    cin>>High;

    cout<<"You will get a set of all prime numbers between "<<Low<<" and "<<High<<"."<<endl;


    while (Low <= High)
    {
        isPrimeNum = isPrime(Low);
        if (isPrimeNum)
            cout<<endl<<Low<<" is prime number.";
        Low++;
    }
}