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;
3int main()
4{
5 int n, i, m=0, flag=0;
6 cout << "Enter the Number to check Prime: ";
7 cin >> n;
8 m=n/2;
9 for(i = 2; i <= m; i++)
10 {
11 if(n % i == 0)
12 {
13 cout<<"Number is not Prime."<<endl;
14 flag=1;
15 break;
16 }
17 }
18 if (flag==0)
19 cout << "Number is Prime."<<endl;
20 return 0;
21}
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
1// 6k+-1 optimisation
2bool is_prime6(int num) {
3 int i;
4 if (num == 1)
5 return false;
6 if (num <= 3)
7 return true;
8 if (num % 2 == 0 || num % 3 == 0)
9 return false;
10 if (num < 25)
11 return true;
12 for (i = 5; i * i <= num; i += 6)
13 if (num % i == 0 || num % (i + 2) == 0)
14 return false;
15 return true;
16}