Home  >  Article  >  Java  >  Detailed introduction to the least common multiple and greatest common divisor in java

Detailed introduction to the least common multiple and greatest common divisor in java

迷茫
迷茫Original
2017-03-26 15:50:432088browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn