C语言程序设计谭浩强版课后习题
8-1
流程图
#include<stdio.h>
int Hfactor(int x,int y)
{
int t;
while(y!=0) {t=x%y;x=y;y=t;}
return x;
}
int Lmultiple(int x,int y)
{
int t,x0,y0;
x0=x;y0=y;
while(y!=0) {t=x%y;x=y;y=t;}
return x0*y0/x;
}
void main()
{
int m,n;
printf("Please intput 2 digits:\n");
scanf("%d,%d",&m,&n);
printf("The highest common factor of m and n is %d,and the least common multiple is %d.\n",Hfactor(m,n),Lmultiple(m,n));
}
8-3
流程图
#include<stdio.h>
#include<math.h>
void prime(int x)
{
int k,i;
k=sqrt(x);
for(i=2;i<=k;i++) if(x%i==0) break;
i>k? printf("m is a prime number.\n",x):printf("m is not a prime number.\n",x);
}
void main()
{
int m;
printf("Please input 1 digit:\n");
scanf("%d",&m);
prime(m);
}