-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime.cpp
36 lines (29 loc) · 854 Bytes
/
prime.cpp
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
/******************************************************************************
Prime no - A prime number is a natural number that is only divisible by 1 and by itself.
ex = 1 2 3 5 7 11 13 17 19
approach -> we wll run a loop from 2 to the total no of elements
- we check if the current no and given n no are divisible or not, if they are mean they are not prime
- so return false, or return true
*******************************************************************************/
#include <iostream>
using namespace std;
bool prime_no(int n){
for(int i=2; i<n; i++){
if(n%i == 0){
return false;
}
}
return true;
}
int main()
{
int n = 11;
bool ans = prime_no(n);
if(n!=1 && ans ==true){
cout<<"it is prime";
}
else{
cout<<"not a prime";
}
return 0;
}