Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我尝试编写两个数字之间的程序打印质数,但是运行时遇到问题。

#include<iostream>

using namespace std;

void prime(int m, int n) {
    int p = 0;
    for (int i = m; i < n; i++)
    {
        for (int i = 1; i < m; i++)
        {
            if (i %m == 0)
                p = p + 1;
        }
        if (p = 2)
            cout << m << endl;
    }
}
int main()
{
    int m, n;
    cout << "enter two numbers" << endl;
    cin >> m >> n;
    prime(m, n);
    return 0;
}

最佳答案

#include<iostream>
using namespace std;
void prime(int m, int n)
{
    for(int i=m; i<n; i++)
    {
        bool isPrimeNumber = false;
        for (int j=1; j<m; j++)
        {
            if(i%j==0&&j!=1) {
                isPrimeNumber = true;
                break;
            }
        }
        if(!isPrimeNumber)
          cout << i << endl;
    }
}


int main()
{
    int m, n;
    cout << "enter two numbers" << endl;
    cin>>m>>n;
    prime(m, n);
    return 0;
}

关于c++ - 两个数字之间的质数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42260372/

10-11 03:58