作业帮 > 综合 > 作业

编程(c/c++)判断素数

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/08/03 20:16:55
编程(c/c++)判断素数
输入格式(遇0结束)
比如:
3
6
7
8
0
输出格式:
yes
no
yes
no
编程(c/c++)判断素数
#include
#include
#include
struct box123
{
int a ;
struct box123 *link ;
} ;
typedef struct box123 box ;
int ifYesNo(int x) ;
int main()
{
int i ;
box *head,*temp1,*temp2 ;
head=(box *)malloc(sizeof(box)) ;
head->link=NULL ;
temp1=head ;
while(1)
{
scanf("%d",&i) ;
if(i==0)
break ;
temp2=(box *)malloc(sizeof(box)) ;
temp1->link=temp2 ;
temp2->link=NULL ;
temp1=temp2 ;
temp1->a=i ;
}
if(head->link==NULL)
{
getch() ;
return 0 ;
}
temp1=head ;
temp2=temp1->link ;
while(temp2!=NULL)
{
ifYesNo(temp2->a) ;
temp2=temp2->link ;
}
getch() ;
return 0 ;
}
int ifYesNo(int x)
{
if(x==1)
{
printf("\nnumber error:cannot input \"1\"") ;
return 1 ;
}
if(x==2)
{
printf("\nyes") ;
return 1 ;
}
if(x%2==0)
{
printf("\nno") ;
return 1 ;
}
for(int i=3;i