1#include <iostream>
2using namespace std;
3
4int main()
5{
6 int n, i;
7 bool isPrime = true;
8
9 cout << "Enter a positive integer: ";
10 cin >> n;
11
12 for(i = 2; i <= n / 2; ++i)
13 {
14 if(n % i == 0)
15 {
16 isPrime = false;
17 break;
18 }
19 }
20 if (isPrime)
21 cout << "This is a prime number";
22 else
23 cout << "This is not a prime number";
24
25 return 0;
26}
1#include <iostream>
2using namespace std;
3
4int main() {
5 int i, n;
6 bool isPrime = true;
7
8 cout << "Enter a positive integer: ";
9 cin >> n;
10
11 // 0 and 1 are not prime numbers
12 if (n == 0 || n == 1) {
13 isPrime = false;
14 }
15 else {
16 for (i = 2; i <= n / 2; ++i) {
17 if (n % i == 0) {
18 isPrime = false;
19 break;
20 }
21 }
22 }
23 if (isPrime)
24 cout << n << " is a prime number";
25 else
26 cout << n << " is not a prime number";
27
28 return 0;
29}
30