C # Prime

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.