C# 求取两个整数的最小公倍数
本文主要介绍使用C#实现求取两个整数的最小公倍数。实现效果如下:

知识点
1、最小公倍数的算法
public float GetMinCommonMultiple(int n1,int n2)
{int temp = Math.Max(n1, n2);n2 = Math.Min(n1, n2);n1 = temp;int Product = n1 * n2;//求两个数的乘积while(n2!=0){n1 = n1>n2? n1:n2;int m = n1 % n2;n1 = n2;n2 = m;}return (Product / n1);}
2、字符串格式化
label1.Text = n1+"和" + n2+"的最小公倍数是"+ GetMinCommonMultiple(n1, n2);
代码
public float GetMinCommonMultiple(int n1,int n2){int temp = Math.Max(n1, n2);n2 = Math.Min(n1, n2);n1 = temp;int Product = n1 * n2;//求两个数的乘积while(n2!=0){n1 = n1>n2? n1:n2;int m = n1 % n2;n1 = n2;n2 = m;}return (Product / n1);}private void button1_Click(object sender, EventArgs e){int n1 = Convert.ToInt16(textBox1.Text.Trim());int n2 = Convert.ToInt16((textBox2.Text.Trim()));label1.Text = n1+"和" + n2+"的最小公倍数是"+ GetMinCommonMultiple(n1, n2);}
