使用 C 语言函数进行 GCD 计算时出错



这段代码显示一些语法错误,我找不到它在哪里,可能是它在gcd函数中。 这是我的代码。

#include<stdio.h>
#include<conio.h>
int main(void)
int gcd(int,int);
{
int a,b, temp;
printf("Enter the value of a");
scanf("%d",&a);
printf("Enter the value of b");
scanf("%d",&b);
if (b>=a)
{
int temp;
temp=b;
b=a;
a=temp;
}
elseif(b!=0)
{
gcd(a,b);
printf("The Gcd of the numbere is %d",gcd(a,b));
}
else
{
printf("The GCD of %d %d is %d:",a,b,a);
}
getch();
return 0;
}
int gcd(int a,int b)
{
int g;
while(b!=0)
{
g=a%b;
a=b;
b=g;
return g;
}
}

如果您指出我的错误并用正确的代码进行解释,我将不胜感激。

切换这两行的位置:

int main(void)
int gcd(int,int);

另外,elseif->else if

gcd函数使用欧几里得算法。它计算a mod b,然后用异或交换交换ab

参考

#include<stdio.h>
int gcd(int ,int );

int main(void)
{
int a,b, temp;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
int res = gcd(a,b);
printf("The Gcd of the numbere is : %d n",res);
return 0;
}
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}

最新更新