-
C++
Prime numbers by using function
A function determines whether an integer is prime or not.
15:18 Dec 19 2010 | Tags :
#include<iostream>
using namespace std;
int prime(int n) {
int i=2;
while(i<n) {
if(n%i==0) {
return 0;
}
i++;
}
return 1;
}
int main() {
int n;
cin>>n;
if(prime(n)==0) {
cout<<n<<" is not a prime number\n";
} else {
cout<<n<<" is a prime number\n";
}
return 0;
}
Add comment
To add a comment, please : Login or Sign up