Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * by: Wualter Vera
- */
- /*
- * 18. Escribir un programa que muestra un menú de opciones que permite elegir alguna
- *de las siguientes tareas:
- *1) Leer 2 números enteros positivos N1 y N2
- *2) Determinar el mínimo común
- *múltiplo de los números
- *3) Determinar el máximo común divisor de los números
- *4)Terminar.
- *El programa se repite mientras no se desee terminar.
- */
- import java.io.*;
- public class Pro_18 {
- static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- Menu();
- Start();
- }
- /**
- * METODO 1 START
- */
- static void Start() throws IOException{
- int op,n1 = 0,n2 = 0,mcm,mcd;
- do {
- op = Opcion();
- if (op==1) {
- n1 = LeerNum1();
- n2 = LeerNum2();
- }else if(op == 2){
- if (n1!=0 && n2 !=0) {
- mcm = CalMCM(n1, n2);
- }else {
- n1 = LeerNum1();
- n2 = LeerNum2();
- mcm = CalMCD(n1, n2);
- }
- EscMCM(mcm, n1, n2);
- //n1 =0;//Reinicio varible para reiniciar el programa
- //n2=0;
- }else if(op == 3){
- if (n1!=0 && n2 !=0) {
- mcd = CalMCD(n1, n2);
- }else {
- n1 = LeerNum1();
- n2 = LeerNum2();
- mcd = CalMCD(n1, n2);
- }
- EscMCD(mcd, n1, n2);
- //n1 =0;//Reinicio varible para reiniciar el programa
- //n2=0;
- }else{
- System.out.print("\n \tGRACIAS...!!!");
- }
- } while (op!=4);
- }
- /**
- * METODO 2 MENU DEL PROGRAMA
- */
- static void Menu() throws IOException{
- System.out.print("\n \tMENÚ DE OPCIONES");
- System.out.print("\n \t================");
- System.out.print("\n1: Leer 2 números: ");
- System.out.print("\n2: Calcular el MCM: ");
- System.out.print("\n3: Calcular la MCD: ");
- System.out.print("\n4: Terminar: ");
- }
- /**
- * METODO 3 OPCIÓN DEL MENÚ
- */
- static int Opcion() throws NumberFormatException, IOException{
- int op;
- do{
- System.out.print("\nDesea continuar? Elija una opción: ");
- op = Integer.parseInt(br.readLine());
- }while(op<=0 || op>4);
- return op;
- }
- /**
- * METODO 4 LEER Num1
- */
- static int LeerNum1() throws IOException{
- int n1;
- do {
- System.out.print("\nIngrese número1: ");
- n1 = Integer.parseInt(br.readLine());
- } while (n1<=0);
- return n1;
- }
- /**
- * METODO 5 LEER Num1
- */
- static int LeerNum2() throws IOException{
- int n2;
- do {
- System.out.print("\nIngrese número2: ");
- n2 = Integer.parseInt(br.readLine());
- } while (n2<=0);
- return n2;
- }
- /**
- * METODO 6 CALCULAR EL MCM
- */
- static int CalMCM(int n1,int n2) throws IOException{
- int d=2,mcm=1;
- while( n1>1 || n2>1){
- if (n1%d == 0 || n2%d == 0) {
- if (n1%d == 0) {
- n1 = n1/d;
- }
- if (n2%d == 0) {
- n2 = n2/d;
- }
- mcm = mcm*d;
- }else{
- d++;
- }
- }
- return mcm;
- }
- /**
- * METODO 7 CALCULAR EL MCD
- */
- static int CalMCD(int n1,int n2) throws IOException{
- int min,mcd =0;
- if(n1 < n2){
- min = n1;
- }else{
- min = n2;
- }
- for(int i=1;i<=min;i++){
- if(n1 % i == 0 && n2 % i == 0){
- mcd = i;
- }
- }
- return mcd;
- }
- /**
- * METODO 8 ESCRIBIR MCM
- */
- private static void EscMCM(int mcm,int n1,int n2) {
- System.out.print("El MCM de "+n1+" x "+n2+" = "+mcm+"\n");
- }
- /**
- * METODO 9 ESCRIBIR MCD
- */
- private static void EscMCD(int mcd,int n1,int n2) {
- System.out.print("El MCD de "+n1+" x "+n2+" = "+mcd+"\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement