jueves, 11 de diciembre de 2014

Ejercicio 69: Calcular el máximo común divisor de dos números mediante el método de Euclídes

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package ejercicio69;
import java.util.Scanner;
/**
*
* @author juan
*/
public class Ejercicio69 {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int num1, num2, mcd=-1,dividendo, divisor, resto;
        Scanner leer = new Scanner(System.in);
        String salir;
        do {
           
       
       
            System.out.println("Introduzca el primer número");
            num1=leer.nextInt();
            System.out.println("Introduzca el segundo número");
            num2=leer.nextInt();
            dividendo=Math.max(num1, num2);
            divisor = Math.min(num1, num2);
            do{
                resto = dividendo % divisor;
                if (resto==0) mcd = dividendo/divisor;
                else {
                    dividendo= divisor;
                    divisor=resto;
                }
            } while (resto!=0 && resto !=1);

            if (resto==0) System.out.println("el mcd es: "+mcd);
            else System.out.println("No hay mcd entre " + num1 + " y "+num2);
            leer.nextLine();
            do{
                System.out.println("Quiere intentarlo otra vez??");
                salir = leer.nextLine();
            } while (salir.isEmpty());
        } while (salir.charAt(0)!= 'n');
    }
   
}

No hay comentarios: