Advertisement
verag

Calcular el MCM y MCD de dos números enteros en java

Jun 26th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. /**
  2.  * by: Wualter Vera
  3.  */
  4. /*
  5.  * 18. Escribir un programa que muestra un menú de opciones que permite elegir alguna
  6.  *de las siguientes tareas:
  7.  *1) Leer 2 números enteros positivos N1 y N2
  8.  *2) Determinar el mínimo común
  9.  *múltiplo de los números
  10.  *3) Determinar el máximo común divisor de los números
  11.  *4)Terminar.
  12.  *El programa se repite mientras no se desee terminar.
  13.  */
  14. import java.io.*;
  15. public class Pro_18 {
  16.     static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.  
  18.     public static void main(String[] args) throws IOException {
  19.         // TODO Auto-generated method stub
  20.         Menu();
  21.         Start();
  22.     }
  23.     /**
  24.      * METODO 1 START
  25.      */
  26.     static void Start() throws IOException{
  27.         int op,n1 = 0,n2 = 0,mcm,mcd;      
  28.         do {
  29.             op = Opcion();
  30.             if (op==1) {
  31.                 n1 = LeerNum1();
  32.                 n2 = LeerNum2();
  33.             }else if(op == 2){
  34.                 if (n1!=0 && n2 !=0) {             
  35.                     mcm = CalMCM(n1, n2);
  36.                 }else {
  37.                     n1 = LeerNum1();
  38.                     n2 = LeerNum2();
  39.                     mcm = CalMCD(n1, n2);
  40.                 }          
  41.                 EscMCM(mcm, n1, n2);
  42.                 //n1 =0;//Reinicio varible para reiniciar el programa
  43.                 //n2=0;
  44.             }else if(op == 3){
  45.                 if (n1!=0 && n2 !=0) {             
  46.                     mcd = CalMCD(n1, n2);  
  47.                 }else {
  48.                     n1 = LeerNum1();
  49.                     n2 = LeerNum2();
  50.                     mcd = CalMCD(n1, n2);  
  51.                 }                      
  52.                 EscMCD(mcd, n1, n2);
  53.                 //n1 =0;//Reinicio varible para reiniciar el programa
  54.                 //n2=0;
  55.             }else{
  56.                 System.out.print("\n \tGRACIAS...!!!");
  57.             }
  58.         } while (op!=4);       
  59.     }
  60.     /**
  61.      * METODO 2 MENU DEL PROGRAMA
  62.      */
  63.     static void Menu() throws IOException{
  64.        
  65.         System.out.print("\n \tMENÚ DE OPCIONES");
  66.         System.out.print("\n \t================");
  67.         System.out.print("\n1: Leer 2 números: ");
  68.         System.out.print("\n2: Calcular el MCM: ");
  69.         System.out.print("\n3: Calcular la MCD: ");
  70.         System.out.print("\n4: Terminar: ");
  71.     }
  72.     /**
  73.      * METODO 3 OPCIÓN DEL MENÚ
  74.      */
  75.     static int Opcion() throws NumberFormatException, IOException{
  76.         int op;    
  77.        
  78.         do{
  79.             System.out.print("\nDesea continuar? Elija una opción: ");
  80.             op = Integer.parseInt(br.readLine());
  81.         }while(op<=0 || op>4);
  82.         return op;
  83.     }    
  84.     /**
  85.      * METODO 4 LEER Num1
  86.      */
  87.     static int LeerNum1() throws IOException{
  88.         int n1;
  89.         do {
  90.             System.out.print("\nIngrese número1: ");
  91.             n1 = Integer.parseInt(br.readLine());  
  92.         } while (n1<=0);       
  93.         return n1; 
  94.     }
  95.     /**
  96.      * METODO 5 LEER Num1
  97.      */
  98.     static int LeerNum2() throws  IOException{
  99.         int n2;
  100.         do {
  101.             System.out.print("\nIngrese número2: ");
  102.             n2 = Integer.parseInt(br.readLine());  
  103.         } while (n2<=0);   
  104.         return n2; 
  105.     }
  106.    
  107.     /**
  108.      * METODO 6 CALCULAR EL MCM
  109.      */
  110.     static int CalMCM(int n1,int n2) throws  IOException{
  111.         int d=2,mcm=1;
  112.         while( n1>1 || n2>1){
  113.             if (n1%d == 0 || n2%d == 0) {
  114.                     if (n1%d == 0) {
  115.                         n1 = n1/d;
  116.                     }
  117.                     if (n2%d == 0) {
  118.                         n2 = n2/d;
  119.                     }
  120.                     mcm = mcm*d;
  121.             }else{
  122.                 d++;
  123.             }
  124.          }
  125.         return mcm;
  126.     }
  127.     /**
  128.      * METODO 7 CALCULAR EL MCD
  129.      */
  130.     static int CalMCD(int n1,int n2) throws  IOException{
  131.         int min,mcd =0;
  132.         if(n1 < n2){
  133.             min = n1;
  134.         }else{
  135.             min = n2;
  136.         }
  137.         for(int i=1;i<=min;i++){
  138.             if(n1 % i == 0 && n2 % i == 0){
  139.                 mcd = i;
  140.             }
  141.         }      
  142.         return mcd;
  143.     }
  144.     /**
  145.      * METODO 8 ESCRIBIR MCM
  146.      */
  147.     private static void EscMCM(int mcm,int n1,int n2) {
  148.         System.out.print("El MCM de "+n1+" x "+n2+" = "+mcm+"\n");
  149.     }
  150.     /**
  151.      * METODO 9 ESCRIBIR MCD
  152.      */
  153.     private static void EscMCD(int mcd,int n1,int n2) {
  154.         System.out.print("El MCD de "+n1+" x "+n2+" = "+mcd+"\n");
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement