• Lenguaje

    JavaScript

  • Descripción

    Solicita N números naturales e imprime la cantidad de números pares y números impares. Además, deberá imprimir el promedio de los números pares y el promedio de los números impares.

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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Cantidad de pares e impares</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script>
 
function algoritmo()
{
    let impares, n, numero_natural, pares;
    let promedio_impares, promedio_pares;
    numero_natural = parseInt (document.formulario1.numero_natural.value);
    if(numero_natural%2==0)
    {
        pares=pares+1;
        promedio_pares=promedio_pares+numero_natural;
    }
    else
    {
        impares=impares+1;
        promedio_impares=promedio_impares+numero_natural;
    }
}
 
        </script>
    </head>
    <body>
        <form name="formulario1">
            <table style="text-align: left; margin-left: auto; margin-right: auto;">
                <tbody>
                    <tr>
                        <td>
                            <label for="numero_natural">Ingresa el valor de numero natural:</label>
                        </td>
                        <td>
                            <input name="numero_natural" required="required" step="1" type="number" />
                        </td>
                    </tr>
                    <tr align="center">
                        <td colspan="2" rowspan="1">
                            <input value="Procesar" type="button" onclick="algoritmo();" />
                            <input type="reset" />
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>