示例代码如下:
- int number1 = 0;
- int number2 = 0;
- Console.WriteLine("请输入两个整数:");
- number1 = int.Parse(Console.ReadLine());
- number2 = int.Parse(Console.ReadLine());
- //最大公约数
- int min = number1 < number2 ? number1 : number2;
for (int i = min; i >= 1; i--) {
if (number1 % i == 0 && number2 % i == 0) {
Console.WriteLine ("最大公约数:" + i);
break;
}
} - //最小公倍数
- int max = number1 > number2 ? number1 : number2;
for (int i = max; i <= number1 * number2; i++) {
if (i % number1 == 0 && i % number2 == 0) {
Console.WriteLine("最小公倍数:" + i);
break;
}
} - Console.ReadKey();