最大公約數:
publicclass CommonDivisor{
publicstaticvoid main(String args[])
{
commonDivisor(24,32);
}
staticint commonDivisor(
int M,
int N)
{
if(N<0||M<0)
{
System.
out.println("ERROR!");
return -1;
}
if(N==0)
{
System.
out.println("the biggest common divisor is :"+M);
return M;
}
return commonDivisor(N,M%N);
}
}
最小公倍數和最大公約數:
import java.util.Scanner;
publicclass CandC
{
//下面的方法是求出最大公約數
publicstaticint gcd(
int m,
int n)
{
while (
true)
{
if ((m = m % n) == 0)
return n;
if ((n = n % m) == 0)
return m;
}
}
publicstaticvoid main(String args[])
throws Exception
{
//取得輸入值
//Scanner chin = new Scanner(System.in);
//
int a = chin.nextInt(), b = chin.nextInt();
int a=23;
int b=32;
int c =
gcd(a, b);
System.
out.println("最小公倍數:" + a * b / c + "\n最大公約數:" + c);
}
}
*