How to judge prime numbers in the C # language
The first thing to understand is the definition of prime number, which means: except for 1 and itself, it cannot be divisible by any other number.
Judgment idea: Establish a cycle, from a=1 start to seek remainder, to A-1 end. Where there is a number remainder of 0, it is judged that the number is not a prime number, and if the remainder result is not 0 until the end of the loop, then it is prime. Therefore, in the loop, it is necessary to use an if judgment to determine whether the residual result is 0.
Implementation code:
BOOL Nums=true
for (int i=2;i<a;i++)
{
if (a%i==0)
{
Nums=false;
}
}
This will determine whether a is a prime number, if you need to output, just see if Nums is true on the line;
Implementation code:
if (nums=true)
{
A is a prime number
}
Else
{
A is not a prime number
}
The idea of prime numbers is a very typical example in the Basic C # course, so it is important to understand this thinking.
Prime number judgments in C #