#include <iostream>
#include <cmath>
usingnamespace std;
bool isPrime(int);
int main(){
int i,x,z;
cout<<"Input a positive integer"<<endl;
cin>>x;
while (x<=0){
cout<<"Input a positive integer"<<endl;
cin>>x;}
if (isPrime(x)){
cout<<isPrime(x)<<"is a prime number."<<endl;
}
else {
cout<<isPrime(x)<<" is not a prime number."<<endl;
}
}
bool isPrime(int){
int x,i;
for (i=2; i<sqrt(x); i++){
if (x%i==0){
returnfalse;}
elsereturntrue;
}}
It just seems like no matter what integer i input (even 5 and 13) i will end up with the boolean function returning me a false or (0) output.
thanks for the advice,however i noticed something wierd,
when input value for x = 2469,
the script returns (true) and indicates that 2469 is a prime number.
but 2469 in fact is not a prime,it is divisible by 3 with no remainder..
does anyone know why does this happen?
yeap, ive done the ammendments as reccomended by mik2718,Thanks alot Mik!! your help is really appreciated!
below is my edited code which is having problem determining whether input value such as 2469 is prime. (while smaller prime numbers like 7,11,13 are working fine..)
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int);
int main(){
int i,x,z;
cout<<"Input a positive integer"<<endl;
cin>>x;
while (x<=0){
cout<<"Input a positive integer"<<endl;
cin>>x;}
if (isPrime(x)){
cout<<x<<" is a prime number."<<endl;
}
else {
cout<<x<<" is not a prime number."<<endl;
}
}
bool isPrime(int x){
int i;
for (i=2; i<floor(sqrt(x)); i++){
if (x%i==0){
return false;}
else
return true;
}}