Judge the prime number between 2-

Source: Internet
Author: User

Before judging the prime numbers between 2-, we should first clearly understand the prime standard:

Prime Number: in a natural number greater than 1, apart from 1 and the integer itself, it cannot be divisible by other natural numbers.

1) Apart from 1 and itself, it cannot be divisible by other natural numbers;

2) 2 is a unique prime number that can be divisible by 2;

3) except for 2, all even numbers are numbers.

C # is implemented as follows:

// 2 is the minimum prime number Console. writeLine (2 + ""); // defines a count variable and calculates the number of prime numbers in 3-. int count = 0; // 2 is already a prime number, therefore, you only need to judge from 3 for (int I = 3; I <= 100; I ++) {// assign I to the variable xint x = I; // define an identity scalar and set the State bool flag to false; // determine whether x meets the condition for (int j = 2; j <x; j ++) {// can be divisible by 2 and any integer smaller than him. It is not a prime number if (x % j = 0) {// if it is not a prime number, flag is set to trueflag = true; // jump out of the loop and determine the next digit break;} if (flag = true) {continue;} // if the flag is false, else {// print the prime number Console. writeLine (x); count ++ ;}// count + 1 calculate all prime numbers in 2-, with a total of 25 consoles. writeLine (count + 1 );

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192633KN-0.gif "alt =" j_0057.gif "/> for beginners, this judgment is still difficult, but with learning, you will understand what is going on.

Come on !!!

This article is from the "Lanting drunk beauty" blog and is not reposted!

Related Article

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.