6

自定义函数打印100到200间的素数

 1 year ago
source link: https://blog.51cto.com/u_15838996/6005812
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

自定义函数打印100到200间的素数

精选 原创

爱莉希雅我的最爱 2023-01-13 10:12:04 ©著作权

文章标签 i++ 自定义函数 文章分类 C/C++ 编程语言 阅读数168

运用的是试除法

#include<stdio.h>
#includie<math.h>//要使用sqrt开根号函数需要的头文件。
int find-prime(int i)//因为最后返回的是1或0要加int
{
int k=0;
for(k = 2;k <= sqrt(i);k++)
{
if(i%k==0)
return 0;
}//这个循环的意思就是当k从2到sqrt(i)都没有一个数让i整除那么这个数就是素数所以最后返回一,若有一个数让其整除它就不是素数返回值为0
return 1;
}//上面的这个自定义函数就是用来判断i是否为素数
int main()
{
int i = 0;
for(i = 100;i <= 200;i++)
{
if(1==find-prime(i))
{
printf("%d ",i);
}
}
return 0;
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK