Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package maximocomundivisor;
- import java.util.Scanner;
- public class MaximoComunDivisor {
- public static void main(String[] args) {
- System.out.println("PROGRAMA MAXIMO COMUN DIVISOR");
- Scanner sc = new Scanner(System.in);
- String dividendo, divisor;
- do{
- System.out.println("INGRESE DIVIDENDO");
- dividendo = sc.nextLine();
- }while(!esNumero(dividendo));
- do{
- System.out.println("INGRESE DIVISOR");
- divisor = sc.nextLine();
- }while(!esNumero(divisor));
- int ddo = Integer.parseInt(dividendo);
- int dsor = Integer.parseInt(divisor);
- System.out.println("Maximo comun divisor " + MCD(ddo, dsor));
- System.out.println("cociente = " + ddo/dsor);
- }
- private static boolean esNumero(String cadena){
- try {
- Integer.parseInt(cadena);
- return true;
- } catch (NumberFormatException excFN){
- System.out.println("Error "+ excFN.getMessage());
- return false;
- }
- }
- public static int MCD(int a, int b){
- if (b == 0){
- return a;
- }
- return MCD(b, a % b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement