i need a help ..... in this code i'm truing to to enter the value x then the program should print message to show whether the x is prime number or not useing for loop
the teacher gave us a hint : you may need to use the idea of flags (Boolean variable)
but when i start the program and enter a value the program stop working plzzzz help me what to do :(
this is my program so plzz help me :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
usingnamespace std;
int main()
{
int x;
cout<<"please enter a vaule :"<<endl;
cin>>x;
for(int i=0;i>=0;i++) // i dont now if the counter is correct
{
if (x%i!=0)// i used the idea of flag like this but i dont know if it is working
cout<<x<<" is a prime"<<endl;
else
cout<<x<<" is a not prime"<<endl;
}
}
In the for loop, loop only until you reach x, since x cannot be divisible by numbers greater than itself. That said, you should check all numbers up to x and if any of them divide x, report it as not prime. Otherwise, report it as prime.
1. major problem - your loop starts at i=0, so you are trying to divide x by 0 which is illegal
2. In your "for" clause the line i>=0 is wrong, it cause an infinite loop, should use for example i=0; i <= x/2 ; i++. Actually there is no need to check remainders after x/2 ;