using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Test
{
public int gcm(int a, int b)
{
int x = 1;
x = a < b ? a : b;
while (x >= 1)
{
if((a%x==0)&&(b%x==0))
{
break;
} x--;
}
return x;
}
public int gcd(int a, int b)
{
int x = 1;
x = a > b ? a : b;
while (x >= 1)
{
if ((x % a == 0) && (x % b == 0))
{
break;
}
x++;
}
return x;
}
class program
{
static void Main(string[] args)
{
Test t = new Test();
Console.WriteLine("请输入一个整数:");
int m = int.Parse(Console.ReadLine());
Console.WriteLine("请输入一个整数:");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("最大公约数:{0},最小公倍数:{1}", t.gcm(m, n), t.gcd(m, n));
}
}
}
}
C#:求两个数的最大公约数和最小公倍数
最新推荐文章于 2024-09-12 15:39:41 发布
该程序定义了一个C#类,包含两个方法:gcm用于计算两个整数的最大公约数(Greatest Common Divisor),gcd用于计算最小公倍数(Least Common Multiple)。用户通过控制台输入两个整数,程序将输出它们的最大公约数和最小公倍数。
摘要由CSDN通过智能技术生成