判断n是否为质数

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
    bool judg = true;
    int i;
    while (cin >> i)
    {
        if (i < 1)
        {
            cout << "请输入1以上的值" << endl;
            continue;
        }

        if (i == 1)
        {
            cout << i << "是质数" << endl;
            continue;
        }

        else if (i == 2)
        {
            cout << i << "不是质数" << endl;
            continue;
        }

        else if (i == 3)
        {
            cout << i << "是质数" << endl;
            continue;
        }


        for (int j = 2; j <= i / 2; j++)
        {
            if (i%j == 0)
            {
                judg = false;
                break;
            }

        }
        if (judg)
        {
            cout << i << "是质数" << endl;
        }
        else
        {
            cout << i << "不是质数" << endl;
            judg = true;
        }

    }
    system("pause");
    return 0;
}

 

posted on 2017-08-27 17:51  那年月光  阅读(599)  评论(0编辑  收藏  举报