If the number is divisible by any of the number which came before it till 2, is NOT a prime. For ex. if 8 is divisible by 7,6,5,4,3,2 then it is NOT a prime.
how can i do that in c++
for example to check if a number is even or odd:
if (number%2 ==0)
cout<< "That number is even" <<endl;
else
cout<< "that number is odd" <<endl;
1. Take the number which to want to check
2. Start a loop from this number-1 till 2
3. Inside the loop check if the given number is divisible by any of the numbers in loop.
4. If yes, then flag = 1 and break from the loop (flag is a normal int or bool variable)
5. else, flag = 0, continue;
6. Outside the loop check if flag = 0 or 1, if 0 then number is prime else not prime
PS Make sure you are breaking from the loop if the number is divisible