C++编写程序,求出两个整数的最大公约数和最小公倍数
#include<stdio.h>
main()
{
int a,b,x,y;
printf("please enter two positive integer number: ");
scanf("%d,%d",&a,&b);
x=f1(a,b);
printf("the greatest common factor is:%d ",x);
y=f2(a,b);
printf("the lease common multiple is:%d ",y);
}
int f1(int a,int b)
{
int t,r;
if(b>a) {t=a;a=b;b=t;}
r=a%b;
while(r!=0)
{
a=b;
b=r
r=a%b;
}
return(b);
}
int f2(int a,int b)
{
int i;
for (i=1;i<=b;i++)
if(i*a%b==0)
return(i*a);
}
编译的时候说f1 f2 undeclared identifier
怎么回事,求大神答疑
C++函数必须先声明,后使用的
你要么将你的f1,f2两个函数(整个)移到main的前面
要么在main()前面加
int f1(int a,int b);
int f2(int a,int b);
另外,C++ main的标准写法为
int main(void)
热门标签: