[ThinkingInC ++] 20. Exercises about C (2)

Source: Internet
Author: User

[ThinkingInC ++] 20. Exercises about C (2)

/*** Function: compile a program to use the two-for Loop and modulo operator (%) to find and output the prime number (only integers divisible by 1 and itself ). * Time: August 15, 2014 08:19:48 * Author: cutter_point */# include

 
  
# Include

  
   
Using namespace std; // write a function output prime number (only integers divisible by 1 and itself *) void printZhiShu (int n) // returns the prime number between 0 and number {cout <1 <"\ t is the prime number \ n"; // uses a two-fold for loop, modulo operator (%), 1 does not need to think // a layer of loop is weighted 2 to n. These numbers are searched for one by one (int I = 2; I <= n; ++ I) {// when the value is 2, only the modulo itself can be modulo 0 for (int j = 2; j <= I; ++ j) {int m = I % j; // modulo j if (j! = I & m = 0) {break; // if it is not a prime number, immediately exclude} cout <

   
    
> I; if (I <0) {cout <"Please do not enter a negative number !! "<

    

   

  

 

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.