You have issues in your second loop, I don’t get the why you used the variable tongchiahet
, The main thing that you have to use is a break statement, which helps you to stop iteration of second loop if so % chia == 0
in between iterations: Here is a working Example for you, And try the following code:
int limit = 20;
for (var so = 1; so < limit; so++)
{
bool isPrime = false;
for (var chia = 2; chia < so; chia++)
{
if (so % chia == 0)
{
isPrime = true;
break;
}
}
if (!isPrime)
Console.WriteLine(so);
}
1
solved sort way to find prime number but it have some error#? [duplicate]