-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisPrime.cpp
56 lines (52 loc) · 1.01 KB
/
isPrime.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <limits>
#include <string>
#include <sstream>
bool isPrime(int x)
{
for (int i = 2; i * i < x; ++i)
{
if (x % i == 0)
{
return false;
}
}
return true;
}
bool isNumber(std::string& ss)
{
std::stringstream iss(ss);
double num;
return (iss >> num) and iss.eof();
}
int main()
{
int i;
std::cout << "Enter number is greten two! ";
while (true)
{
std::string input;
std::cin >> input;
if (isNumber(input))
{
i = std::stoi(input);
if (i > 1) {
break;
}
}
else
{
std::cout << "Invalid input: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
if (isPrime(i))
{
std::cout << "Number (" << i << ") is prime:\n";
}
else {
std::cout << "Number (" << i << ") is not prime:\n";
}
return 0;
}