import javax.swing.JOptionPane; public class MassimoComunDivisore { public static void main(String[] args) { int x, y, ris; String s = JOptionPane.showInputDialog("Digita un intero positivo"); x = Integer.parseInt(s); s = JOptionPane.showInputDialog("Digita un altro intero positivo"); y = Integer.parseInt(s); ris=MCD(x,y); JOptionPane.showMessageDialog(null, "Il MCD tra "+x+" e "+y+" e' "+ris); System.exit(0); } public static int MCD(int a, int b) /* a e b sono positivi qualsiasi * ma il metodo MCDEuclide richiede che il primo parametro * non sia maggiore del secondo */ { if(a>=b) return MCDEuclide(a,b); else return MCDEuclide(b,a); } public static int MCDEuclide(int a, int b) /* assume a>=b */ { int resto = a%b; if (resto==0) return b; else return MCDEuclide(b, resto); } }