Record: Find the prime number between 1 and 100

Source: Internet
Author: User

Train of Thought Analysis:1) First, determine the number of prime numbers. A prime number is a number that can be divisible by 1 and itself, but cannot be divisible by a number smaller than 1;

2) 2 is the smallest prime number and the only prime number that can be divisible by 2;

3) All the even numbers except 2 are not prime numbers.

In C #, we usually write the following program:

// Find the prime number between 1 and 100.

Console. Write (2 + "");

For (int x = 3; x <= 100; x + = 2) // if it is an even number, do not consider

{

Bool flag = true; // The dozens of prime numbers to be judged by default

If (x % 2 = 0)

{

Flag = false;

}

Else

{

For (int I = 3; I <x/2; I + = 2)

{

If (x % I = 0)

{

Flag = false;

Break;

}

}

}

If (flag = true) // the number currently determined is indeed a prime number.

{


Console. Write (x + ""); // Add a space to the end of x.

}

}

Console. WriteLine ();


For many masters, this may be as simple as "1 + 1 = 2", but for a beginner like me, I have to worry about it! Come on, Ajax girl! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/20362V3R-0.gif "/>

This article is from the "Ajax girl" blog!

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.