如何用C++编程,输入一个整数m,判断其是否为素数。

日期:2018-03-28 16:48:38 人气:1

如何用C++编程,输入一个整数m,判断其是否为素数。

思路:定义一个函数,传入参数n,从2到n-1依次递增整除n如果能整除返回0,表示n不是素数,否则返回1,表示n是素数。 参考代码: #include using namespace std;int fun(int n){ int i; for(i=2;i>n; if(fun(n)) cout<<n<<"是素数"<<endl; else cout<<n<<"不是素数"<<en
    A+
热门评论