Prime number judgments in C #

Source: Internet
Author: User

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 #

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.