Advertisement
Mitoeap

Untitled

Aug 29th, 2020
1,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package maximocomundivisor;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class MaximoComunDivisor {
  7.  
  8.  
  9.     public static void main(String[] args) {
  10.            
  11.         System.out.println("PROGRAMA MAXIMO COMUN DIVISOR");
  12.         Scanner sc = new Scanner(System.in);
  13.         String dividendo, divisor;
  14.  
  15.         do{
  16.             System.out.println("INGRESE DIVIDENDO");
  17.             dividendo = sc.nextLine();
  18.         }while(!esNumero(dividendo));
  19.  
  20.         do{
  21.             System.out.println("INGRESE DIVISOR");
  22.             divisor = sc.nextLine();
  23.         }while(!esNumero(divisor));
  24.    
  25.         int ddo = Integer.parseInt(dividendo);
  26.         int dsor = Integer.parseInt(divisor);
  27.         System.out.println("Maximo comun divisor " + MCD(ddo, dsor));
  28.        
  29.         System.out.println("cociente = " + ddo/dsor);
  30.     }
  31.    
  32.     private static boolean esNumero(String cadena){
  33.     try {
  34.         Integer.parseInt(cadena);
  35.                
  36.         return true;
  37.     } catch (NumberFormatException excFN){
  38.                 System.out.println("Error "+ excFN.getMessage());
  39.         return false;
  40.     }
  41.     }
  42.      
  43.     public static int MCD(int a, int b){
  44.         if (b == 0){
  45.             return a;
  46.         }    
  47.         return MCD(b, a % b);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement