Home > Java > javaTutorial > body text

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

迷茫
Release: 2017-03-26 15:50:43
Original
2100 people have browsed it

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));
        }
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template