在本教程中,我们将讨论一个程序,以查找给定间隔之间的质数。
为此,我们将提供两个整数。我们的任务是找到该特定范围内的质数。
#include <bits/stdc++.h> using namespace std; int main() { int a, b, i, j, flag; //射程越来越低 a = 3; //达到上限 b = 12; cout << "\nPrime numbers between " << a << " and " << b << " are: "; for (i = a; i <= b; i++) { if (i == 1 || i == 0) continue; flag = 1; for (j = 2; j <= i / 2; ++j) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) cout << i << " "; } return 0; }
输出结果
Prime numbers between 3 and 12 are: 3 5 7 11