使用欧几里得算法求两个自然数的最大公约数

来源:互联网 发布:centos 6.8安装 分区 编辑:程序博客网 时间:2024/05/21 06:20
import java.util.Scanner;
public class Demo2 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入两个自然数:");
        int m = s.nextInt();
        int n = s.nextInt();
        commFactor(m,n);
    }
    public static void commFactor(int m,int n){
        int r = m%n;
        while(r!=0){
            m = n;
            n = r;
            r = m%n;
        }
        System.out.println("两个自然数的最大公约数为:"+n);

    }

}


原创粉丝点击