Home Foros Foro dudas Foro de JavaScript Programa JavaScript calcular el factorial de un numero

Programa JavaScript calcular el factorial de un numero

Viendo 1 entrada (de un total de 1)
  • Autor
    Entradas
  • #11462

    rubcr
    Participante

    <span style=»background-color: #ffffc8; color: #000000; font-family: Verdana; font-size: 12px;»>Hola a todos, tengo que hacer un script que calcule el factorial de un número.</span>

    <span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>Aparte debe mostrar por consola toda la operación, por ejemplo si se introduce un 4:</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>1.Calculando Factorial de 4</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>2.Operación = 4x3x2x1</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>3.Factorial = 24</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>La parte del script que lo calcule ya lo tengo y funciona</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>Código:</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»><script></span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>var n1,n2;</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>    n1=parseInt(prompt(«Introduce un número»));</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>    n2=1;</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>if(n1<0){</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>    while(n1<0){</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>        n1=parseInt(prompt(«El valor del número introducido debe ser mayor que 0″));</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>    }</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>}</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>while (n1>1){</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>    n2=n1*n2;</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>    n1=n1-1;</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>}</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>document.write(«El factorial es: «+n2);</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»></script></span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>La parte de la consola no sé como implementarlo.</span><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><br style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;» /><span style=»color: #000000; font-family: Verdana; font-size: 12px; background-color: #ffffc8;»>Espero que alguien pueda ayudarme, un saludo.</span>

Viendo 1 entrada (de un total de 1)
  • Debes estar registrado para responder a este debate.