Determine the number of prime numbers between-and output all prime numbers.

Source: Internet
Author: User

Question: determine the number of prime numbers between-and output all prime numbers.

Program Analysis: first understand what is a prime number, only the number divisible by 1 and itself, traverse the number between 101 and in a loop, and then use ~ For example, if the number is 200, we divide the number from 2 to 2 ~ The number between 112, as long as the integer here is not equal to 0, you can judge that this number is a prime number;

# Include <stdio. h> int main () {int I, j; int COUNT = 0; for (I = 101; I <= 200; I ++) {for (j = 2; j <I; j ++) {// If J can be rounded up by I in the out-of-loop if (I % J = 0) break;} // determine whether the loop exists in advance, if j <I indicates 2 ~ Between J, I has an integer. If (j> = I) {count ++; printf ("% d", I); // line feed, count with count, if (count % 5 = 0) printf ("\ n") ;}} return 0 ;}

Running result:

The other is to use a number to remove 2 to SQRT (This number). If it can be divisible, it indicates that this number is not a prime number, and vice versa.

#include <stdio.h>#include <math.h>int main(){    int i,j,k,leap=1;    int count=0;        for (i=101; i<=200; i++)     {        k=sqrt(i+1);        for (j=2; j<=k; j++)         {            if (i%j==0)             {                leap=0;                break;            }        }                if (leap)         {            count++;            printf("%d ",i);            if (count % 5 == 0)             printf("\n");        }                leap=1;    }        return 0;}

Running result:




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.