Latest web development tutorials

C Exercise Examples 36 - find prime numbers within the 100

100 cases of classic C language 100 cases of classic C language

Title: Find the prime numbers within the 100.

Program analysis: prime (prime number), also known as primes, there are an infinite number.A natural number greater than 1, except 1 and itself, the other can not be a natural number divisible by.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

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

The above example output is:

2 3 5 7 11 
13 17 19 23 29 
31 37 41 43 47 
53 59 61 67 71 
73 79 83 89 97 

100 cases of classic C language 100 cases of classic C language