prime numbers

I want to create a program to decide whether is number is prime or not, and i tried with this code, but my program does not seem to go into the while loop and come out with the my statements (i took them out for the forum)... does anyone have an idea why?

int main(){
int num;
int i=2;

cin >> num;

while (num % i != 0 && i<=num){
if(num % i == 0){
statement not prime
}
if(i == num){
statement is prime
}
i++;
}

}

Because you test for whether num%i!=0 at each step. So the neither if statement will ever be encountered in a situation where they can be true.
okay so i switched it to

while (bool1 == false && bool2 == false && i <= num){
if(i == num){
bool1 = true; (if it is prime)
}
if(num % i == 0){
bool2 = true (if not prime)
}
i++;
}

but it still doesnt seem to change the bools
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){

bool is_prime=true;
int num;
cin >> num;

for (int i=2;i<num;++i)
{
if (num%i==0){is_prime=false;break;}
}

return 0;
}
Last edited on
Topic archived. No new replies allowed.