• Lenguaje

    Pascal

  • Descripción

    Pide un número entero y muestra su valor en letra

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

const unidades : array[1..9] of string = ('uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete', 'ocho', 'nueve');
const decimas  : array[1..9] of string = ('diez', 'veinte', 'treinta', 'cuarenta', 'cincuenta', 'sesenta', 'setenta',
                                          'ochenta', 'noventa');
const diez_y   : array[1..5] of string = ('once', 'doce', 'trece', 'catorce', 'quince');
const centenas : array[1..9] of string = ('ciento', '', '', '', 'quinientos', '', 'setecientos', '', 'novecientos');

procedure unidades_en_letra (cantidad : longint);
var unidad, decima, centena : byte;
begin
    unidad  := cantidad mod 10;
    decima  := (cantidad div 10) mod 10;
    centena := (cantidad div 100) mod 10;
    if centena <> 0 then
        if centenas[centena] = '' then
            write (unidades[centena], 'cientos')
        else if (centena <> 1) or (unidad <> 0) or (decima <> 0) then
            write (centenas[centena])
        else
            write ('cien');
    if (unidad <> 0) or (decima <> 0) then
    begin
        if centena <> 0 then
            write (' ');
        if decima = 0 then
            write (unidades[unidad])
        else if unidad = 0 then
            write (decimas[decima])
        else if (decima = 1) and (unidad>=1) and (unidad<=5) then
            write (diez_y[unidad])
        else if decima = 1 then
            write ('dieci', unidades[unidad])
        else if decima = 2 then
            write ('veinti', unidades[unidad])
        else
            write (decimas[decima], ' y ', unidades[unidad]);
    end;
end;

procedure millares_en_letra (cantidad : longint);
var unidades, miles : longint;
begin
    unidades := cantidad mod 1000;
    miles    := (cantidad div 1000) mod 1000;
    if miles > 1 then
        unidades_en_letra (miles);
    if miles <> 0 then
        write ('mil');
    if (unidades <> 0) and (miles <> 0) then
        write (' ');
    unidades_en_letra (unidades);
end;

procedure millones_en_letra (cantidad : longint);
var unidades, millares : longint;
begin
    unidades := cantidad mod 1000000;
    millares := (cantidad div 1000000) mod 1000000;
    if millares > 1 then
    begin
        millares_en_letra (millares);
        write (' millones');
    end
    else if millares <> 0 then
        write ('un mill'#162'n');
    if (unidades <> 0) and (millares <> 0) then
        write (' ');
    millares_en_letra (unidades);
end;

var cantidad : longint;
var tecla : char;
begin
    write ('Ingrese una cantidad: ');
    readln (cantidad);
    writeln;
    if cantidad < 0 then
        begin
            write ('menos ');
            cantidad := -cantidad;
        end;
    if cantidad = 0 then
        write ('cero')
    else
        millones_en_letra (cantidad);
    write (#10#13#10#13'Presione una tecla para terminar . . . ');
    tecla := readkey;
end.