forked from fineanmol/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.cpp
More file actions
37 lines (29 loc) · 787 Bytes
/
prime.cpp
File metadata and controls
37 lines (29 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// C++ Program to check for prime number using
// Simple Trial Division
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
cout << n << " is NOT prime" << endl;
else {
// Check for divisors from 1 to n
for (int i = 1; i <= n; i++) {
// Check how many number is divisible
// by n
if (n % i == 0)
cnt++;
}
// If n is divisible by more than 2 numbers
// then it is not prime
if (cnt > 2)
cout << n << " is NOT prime" << endl;
// else it is prime
else
cout << n << " is prime" << endl;
}
return 0;
}