C language determines the number of all prime numbers within 300 and outputs all prime numbers.

#Judge the number of all prime numbers within 300 and output all prime numbers.
##Code part:

#include <stdio.h>
int han(int n)          							//定义一个判断素数的函数
{
    int i;
    for(i=2;i<n;i++)    									//利用循环
        if(n%i==0)
            return 0;   								//如果不是素数的话,返回值为0
        return 1;       					//如果是素数的话,返回值为1    
}

int main()
{int a=0,n;
    printf("300以内的素数为:");
    for(n=300;n>1;n--) 							//利用之前编写的函数来找到300以内的素数
    {
        if(han(n)==1)
        {
            printf("%d ",n);
            a=a+1;      								//用a的值来统计素数的个数
        }
    }
    printf("\n");
    printf("300以内的素数个数为:%d\n",a);
    system("pause");

    return 0;
}

おすすめ

転載: blog.csdn.net/Deng7326/article/details/115239451