-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathisPrime.cpp
More file actions
42 lines (36 loc) · 735 Bytes
/
isPrime.cpp
File metadata and controls
42 lines (36 loc) · 735 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
38
39
40
41
42
/* This program finds difference of highest and
lowest prime no's in user entered range.
*/
#include <iostream>
using namespace std;
// To check whether number is prime or not
bool isPrime(int num) {
if(num <= 1){
return false;
}
else if (num == 2){
return true;
}
else if (num%2 == 0){
return false;
}
else{
for(int i = 3; i < sqrt(num); i+=2) {
if(num%i== 0)
return false;
}
return true;
}
}
// Main function
int main() {
int num;
cin >> num;
if (isPrime(num)) {
cout << num << " is a prime number.!";
}
else {
cout << num << " is not a prime number.!";
}
return 0;
}