判断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; }
step by step.