
MCM Y MCD en c++ o java
Publicado por Kevin (1 intervención) el 05/10/2016 01:24:16
Tengo problema necesito un programa que me calcule el maximo comun divisor y minimo comun multiplo por descomposicion prima pero no me muestra los datos correctamente, adjunto mi codigo ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* 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 tarea;
import java.util.Scanner;
/**
*
* @author kevin
*/
public class TAREA {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int opcion;
Scanner ingreso = new Scanner (System.in);
do{
System.out.println("1) Ingresar");
System.out.println("2) MCD");
System.out.println("3) MCM");
System.out.println("4) Salida");
opcion = ingreso.nextInt();
switch(opcion){
case 1:
case 2: maximo();
break;
case 3: minimo();
break;
default:
}
}while(opcion <4);
}
public static void maximo(){
String tempa="";
String tempb="";
Scanner in = new Scanner(System.in);
int a, b, mcd=1, divisor;
System.out.print("Ingrese el valor de A: ");
a = in.nextInt();
System.out.print("Ingrese el valor de B: ");
b = in.nextInt();
if (a<0)
a = -a;
if (b<0)
b = -b;
for (divisor=2; divisor<=a && divisor<=b; divisor++)
while (a%divisor==0 && b%divisor==0){
mcd *= divisor;
tempa= a+"^"+divisor;
tempb= b+"^"+divisor;
a /= divisor;
b /= divisor;
}
System.out.println(tempa);
System.out.println(tempb);
System.out.println("Maximo Comun Divisor: " + mcd+ "\n");
}
public static void minimo(){
ingreso();
}
public static void ingreso(){
Scanner in = new Scanner(System.in);
int a, b, divisor;
long mcm = 1;
System.out.print("Ingrese el valor de A: ");
a = in.nextInt();
System.out.print("Ingrese el valor de B: ");
b = in.nextInt();
if (a<0)
a = -a;
if (b<0)
b = -b;
for (divisor=2; divisor<=a || divisor<=b; divisor++)
while ((divisor<=a && a%divisor==0) || (divisor<=b && b%divisor==0)) {
mcm *= divisor;
if (divisor<=a && a%divisor==0)
a /= divisor;
if (divisor<=b && b%divisor==0)
b /= divisor;
}
System.out.println("Minimo Comun Multiplo: " +mcm+ "\n");
}
}
Valora esta pregunta


0