递归求最大公约数

 
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;
        
    }

}

 


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

posted @ 2014-08-14 17:04  Amoxicil  阅读(379)  评论(0编辑  收藏  举报