1. What is a prime number, which is only divisible by 1 and by itself, is called Prime.
such as :
17/1=17;
17/17=1;
2, then understand the prime number is what, let's take a look at C # inside the use of loops, to find prime numbers;
All prime numbers between outputs 100-200 (judging prime numbers using a Count method)
For (int i=100;i<=200;i++)
{
bool Isfind=false;
for (int j=2;j<i;j++)
{
if (i%j==0)
{
isfind=true;
}
}
if (!isfind)
{
Console.WriteLine (i);
}
}
console.readline ();
1. The range is between 100-200 , so we're going to take each number in the interval and try it out, all the prime numbers that exist in the last output interval.
2. To verify the change in each number, we first define the number of a bool type in the beginning of the code. type bool has only two results, one is true: true, one is false: false;
3. With a For loop, define intervals of 100-200, one cycle at a time, then this cycle will be the next new loop, so the back is i++;
4. We know that prime numbers can only be divisible by themselves and by themselves, so we nest a loop for, he represents the result of redundancy between I and J, if 0, then bool outputs true: TRUE, but the true number is not the prime that we want, sothe end: break; The following when I and J's redundancy does not satisfy the if (i%j==0) condition, then this number is the prime we are looking for, but his output is false:false, we use if judgment! (Take the opposite meaning), that is, false to reverse that is true, and finally we output Console.WriteLine (i); Finally get the results we want.
C # Prime