一、两个整数的最大公约数程序:
#include <stdio.h>
int main()
{
int a, b, max, min, i;
printf("请输入数字a的值:");
scanf_s("%d", &a);
printf("请输入数字b的值:");
scanf_s("%d", &b);
if (a > b)
{
max = a;
min = b;
}
else
{
max = b;
min = a;
}
i = min+1;
do
{
--i;
} while ((max % i != 0) || (min % i != 0));
printf("上面两个数字的最大公约数为%d
",i);
return 0;
}
二、两个整数的最小公倍数程序(注:两个整数的最小公倍数是两个整数的积除以这两个整数的最大公约数):
#include<stdio.h>
int main()
{
int a = 0, b = 0, n = 0, x = 0;
int max = 0, min = 0;
printf("请输入整数a:");
scanf_s("%d",&a);
printf("请输入整数b:");
scanf_s("%d", &b);
if (a > b)
{
max = a;
min = b;
}
else
{
max = b;
min = a;
}
n = a+1;
do
{
--n;
} while ((b % n != 0) || (a % n != 0));
x = (a * b) / n;
printf("它们的最小公倍数为:%d
",x);
return 0;
}