最大公约数和最小公倍数(C语言)

时间:2024-04-19 07:04:43

一、N-S流程图;

二、运行结果;

三、源代码;

# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>

//实现最大公约数函数;
int max(int x, int y)
{
	//初始化变量值;
	int judge = 1;

	//运算;
	judge = x % y;

	//循环运算;
	while (judge != 0)
	{
		//转换值;
		x = y;
		y = judge;

		//运算;
		judge = x % y;
	}

	//返回值;
	return y;
}

int main()
{
	//初始化变量值;
	int m = 0;
	int n = 0;
	int result = 0;

	//提示用户输入数据;
	printf("请输入两个整数m n:");
	//获取用户输入的数据;
	scanf("%d %d", &m, &n);

	//判断分母;
	if (m > n)
	{

	}
	else
	{
		//交换位置;
		result = m;
		m = n;
		n = result;
	}

	//调用求最大公约数的函数;
	result = max(m, n);

	//输出结果;
	printf("m=%d, n=%d的最大公约数为:%d,", m, n, result);

	//计算最小公倍数;
	m = m / result;
	n = n / result;
	result = result * m * n;

	//输出结果;
	printf("最小公倍数为:%d\n", result);



	return 0;
}