Home >Java >javaTutorial >Detailed introduction to the least common multiple and greatest common divisor in java
Least common multiple and greatest common divisor in java
import java.util.Scanner; /** * Created by Admin on 2017/3/26. */ public class test02 { public static int MinCommon(int a, int b) { int c, m = a * b; if(a<b){ int t=a; a=b; b=t; } if (a % b == 0) return a; else { while (b != 0) { c = a % b; a = b; b = c; } return m/a; } } public static void main(String[] args) { int a=1,b=1; Scanner scanner=new Scanner(System.in); while (a!=0||b!=0){ a=scanner.nextInt(); b=scanner.nextInt(); int result=MinCommon(a,b); System.out.println(result); System.out.println((a*b/result)); } } }
The above is the detailed content of Detailed introduction to the least common multiple and greatest common divisor in java. For more information, please follow other related articles on the PHP Chinese website!