注册 登录
编程论坛 C++教室

判断素数出问题!不知道哪出错了。

boy_royalty 发布于 2011-10-15 17:29, 458 次点击
下面是我写的代码,求教指点
#include<iostream.h>
#include<math.h>
int main()
{
    int a;
    cin>>a;
    for(int i=2;i<=sqrt(a);i++)
    {
        if(a%i==0)
            break;
    }
    if(i>=sqrt(a)+1)
    {
        cout<<"yes"<<endl;
    }
    else
    {
        cout<<"no"<<endl;
    }
    return 0;
}
2 回复
#2
nomify2011-10-15 18:31
sqrt(a)前面加(int)强制类型转换。
#3
yy7software2011-10-15 18:39
程序代码:
#include<iostream.h>
#include<math.h>
int main()
{
    int a;
    cin>>a;
    for(int i=2;i<=sqrt(a);i++)
    {
        if(a%i==0)
            break;
    }
    if(i>sqrt(a))
    {
        cout<<"yes"<<endl;
    }
    else
    {
        cout<<"no"<<endl;
    }
    return 0;
}
你看下

1