利用辗转相除法求最大公约数;
求最小公倍数。
import java.util.Scanner;
public class Test
{
/**
*利用辗转相除法求最大公约数
*/
public static int gcd(int a,int b){
int m,n,r;
if(a>b){
m = a;
n = b;
}else{
m = b;
n = a;
}
r = m%n;
while(r!= 0){
m = n;
n = r;
r = m%n;
}
return n;
}
/**
*求最小公倍数
*/
public static void lcm(int a,int b){
for(int i = 1;i<=a*b;i++){
if(i%a==0&&i%b==0){
System.out.println("a和b的最小公倍数为:"+i);
break;
}
}
}
public static void main(String[] args)
{
int a ,b,c,d;
System.out.println("输入两个正整数:");
Scanner s = new Scanner(System.in);
a = s.nextInt();
b = s.nextInt();
c = gcd(a,b);
lcm(a,b);
System.out.println("a和b的最大公约数为:"+c);
}
}