Write a function that determines whether an integer is a prime number.
#include <iostream>
using namespace std;
bool is_prime(int a){
if(a <= 1) return false;
else if(a == 2) return true;
else{
for(int i = 2; i < a;i++){
if(a % i == 0) return false;
}
return true;
}
}
int main(){
int input;
cout<<"Input an integer: ";
cin>>input;
cout<<input<<" is";
if(is_prime(input)) cout<<" prime.";
else cout<<" not prime.";
return 0;
}
Comments
Leave a comment