首页 > 代码库 > 递归求最大公约数

递归求最大公约数

 
import java.io.IOException;import java.util.Scanner;public class CommonDivisor {    public static void main(String[] args)throws IOException{        try{             System.out.println("请输入两个整数!");                Scanner input = new Scanner(System.in);                int x = input.nextInt();                int y = input.nextInt();                System.out.println("两个整数的最大公约数为:" + gcd(x,y));        }        catch(Exception ex){            System.out.println("需要输入的数为整数!");        }    }    public static int gcd(int m,int n){        int result = 0;        if(m % n ==0){            result = n;        }        else            result = gcd(n,m % n);        return result;            }}

 

用递归,求两个整数的最大公约数。