Im writing a program that uses a thread when you enter a number,displays its prime numbers.the program runs but it just displays all of the numbers in order and not just the primes.
Well, by looking at the code, I think the problem is DWORD(*pNum) % 2 == 0)
I think DWORD(*pNum) is even number, and since you are always dividing it by the same number and also you are not breaking from the loop when the number is divisible. Therefore, it is printing out all the numbers. I guess the logic should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int flag = 1;
for (int i = 2; i < *pNum; i++) {
if (DWORD(*pNum) % i == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
cout<<*pNum<<" is a prime number";
}
else {
cout<<*pNum<<" is not a prime number";
}