#include <iostream>
int main()
{
std::cout << "Please input a positive integer: ";
int number;
std::cin >> number;
if(number < 0)
{
std::cout << "Error: integer is negative\n";
return 1;
}
if(number < 2)
{
std::cout << "Integer is not prime\n";
return 0;
}
if(number == 2)
{
std::cout << "Integer is prime\n";
return 0;
}
if(number % 2 == 0)
{
std::cout << "Integer is not prime\n";
return 0;
}
for(int i=3; (i*i)<=number; i+=2)
{
if(number % i == 0)
{
std::cout << "Integer is not prime\n";
return 0;
}
}
std::cout << "Integer is prime\n";
return 0;
}
Comments
Leave a comment