Skip to content

Commit 88dc029

Browse files
authored
Check for Prime number
Write a C++ program to accept an integer number and test whether it is prime or not.
1 parent 28d5410 commit 88dc029

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: identifyPrime.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <cmath>
2+
#include <iostream>
3+
bool identifyPrime(int);
4+
5+
6+
int main() {
7+
int num;
8+
std::cout << "Enter a number: "; std::cin >> num;
9+
if (identifyPrime(num))
10+
std::cout << std::endl << num << " is a prime number";
11+
else
12+
std::cout << std::endl << num << " is not a prime number";
13+
return 0;
14+
}
15+
16+
17+
bool identifyPrime(int a) {
18+
bool isPrime = true;
19+
float sqrt_a = sqrt(a);
20+
for (int i = 2; i < sqrt_a; i++) {
21+
if (a%i == 0) {
22+
isPrime = false;
23+
break;
24+
}
25+
}
26+
return isPrime;
27+
}

0 commit comments

Comments
 (0)