package com.homework.test;
import java.util.*;
/*
【程序 6 求最大公约数及最小公倍数】
题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
*/
public class gcd_lcm {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Please input two numbers:");
int num1 = in.nextInt();
int num2 = in.nextInt();
in.close();
int gcd = Gcd(num1,num2);
int lcm = Lcm(num1,num2);
System.out.println("The GCD is "+gcd);
System.out.println("The LCM is "+lcm);
}
//最大公约数 ——> 辗转相除法
public static int Gcd(int m,int n){
int temp;
if(m
{
temp = m;
m = n;
n = temp;
}
if(n == 0) //余数为零
return m; //此时m为上一次除法中较小的数即n
else
return Gcd(n, m%n);//递归调用
}
//最小公倍数
public static int Lcm(int m,int n){
return (m*n)/Gcd(m,n);
}
}