易百教程

用C语言编写一个检查素数的程序?

参考以下代码实现:

#include<stdio.h>      
#include<conio.h>      
void main()      
{      
    int n,i,m=0,flag=0;    //declaration of variables.  
    clrscr();    //It clears the screen.  
    printf("Enter the number to check prime:");      
    scanf("%d",&n);      
    m=n/2;      
    for(i=2;i<=m;i++)      
    {      
        if(n%i==0)      
        {      
            printf("Number is not prime");      
            flag=1;      
            break;    //break keyword used to terminate from the loop.  
        }      
    }      
    if(flag==0)      
        printf("Number is prime");      
    getch();    //It reads a character from the keyword.  
}