• Lenguaje

    Pascal

  • Descripción

    Pide 2 números enteros y calcula el máximo común divisor

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
program maximo_comun_divisor;
uses crt;

var a, b, mcd, divisor : integer;
var tecla : char;
begin
    mcd := 1;
    write ('Ingrese el valor de A: ');
    readln (a);
    write ('Ingrese el valor de B: ');
    readln (b);
    if a<0 then
        a := -a;
    if b<0 then
        b := -b;
    divisor := 2;
    while (divisor<=a) and (divisor<=b) do
        begin
            while (a mod divisor=0) and (b mod divisor=0) do
                begin
                    mcd := mcd * divisor;
                    a := a div divisor;
                    b := b div divisor;
                end;
            inc (divisor);
        end;
    writeln ('M'#160'ximo com'#163'n divisor: ', mcd);
    write (#10#13'Presione una tecla para terminar . . . ');
    tecla := readkey;
end.