Problem with do while in factorial

0

Good I am doing a program that gives you the factorial of a number, I have it resolved in a for and while but when wanting to pass it to do while it does not throw me anything, I do not know why it is unknown a lot about do while .

This is with for :

<link rel="stylesheet" href="tablass.css">
<div class="container">
  <h2>EJERCICIO DEL FACTORIAL</h2>
  <p>Factoriales de numeros</p>            
  <table class="table">    

    <thead>         
       <th></th>
       <th></th>
       <th>   </th>
       <th>  </th>
       <th>  </th>
    </thead>

    <tbody>     

<?php       
  echo"<table border=1 cellspacing=0 width=200";
  echo "<tr><th colspan=5> Factorial </th></tr>";   
  $factorial=1;
  $i;
  $numero=$_REQUEST['valor1'];

  for($i=1;$i<=$numero;$i++){
      echo "<tr>";

      $factorial=$factorial*$i;

      echo "<td>".$factorial. "</td>";      
  }    
?>

<!DOCTYPE html>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
<html lang="es">
<link rel="stylesheet" href="tablass.css">
<head>    
<title> </title>    
</head>
<body>
<h1>Factorial del 5 </h1>
<form action="factorial.php" method="post">
<h1>Ingresa un numero</h1>:
<input type="text" name="valor1"><br>
<input type="submit" value="Aceptar">
</form>
</body>

echo"<table BORDER='1'>"; 

    echo"
    <tr>
    <td colspan=7 align=center>Factorial
    </td>
    </tr>

    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>

This is with while :

<link rel="stylesheet" href="tablass.css">
<div class="container">
  <h2>EJERCICIO DEL FACTORIAL</h2>
  <p>Factoriales de numeros</p>            
  <table class="table">     
    <thead>         
       <th></th>
       <th></th>
       <th></th>
       <th></th>
       <th></th>
    </thead>
    <tbody>

<?php

  echo"<table border=1 cellspacing=0 width=200";
  echo "<tr><th colspan=5> Factorial </th></tr>";   
  $factorial=1;
  $i;
  $numero=$_REQUEST['valor1'];

  while($i<=$numero){
     echo "<tr>";    
     $factorial=$factorial* $i;       
     echo "<td>" .$factorial. "</td>";    
     $i++;    
  }    
?>

Here I have the problem with do while :

<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
<html lang="es">

<link rel="stylesheet" href="tablass.css">
<div class="container">
  <h2>EJERCICIO DEL FACTORIAL</h2>
  <p>Factoriales de numeros</p>            
  <table class="table">     
    <thead>         
       <th></th>
       <th></th>
       <th></th>
       <th></th>
       <th></th>
    </thead>
    <tbody>

<?php       
  echo"<table border=1 cellspacing=0 width=200";
  echo "<tr><th colspan=5> Factorial </th></tr>";    
  $factorial=1;
  $numero=$_REQUEST['valor1'];
  $i;
  do{    
      echo "<tr> <td align=center>$factorial</td>
             <td align=center>x</td>
             <td align=center>$i</td>
             <td align=center>=</td>
             <td align=center> ". $factorial*$i . "</td>
            </tr>";

      $factorial=$factorial*$i;
      echo "<td> ".$factorial. " </td>";    
      $i++;      
  } while($i<=numero)    
?>

    </tbody>
  </table>
</div>


</body>
</html>
  

Note: It does not show the same result, but it does not mark me error

    
asked by wdwd 20.04.2018 в 21:32
source

1 answer

1

Your code is not very readable but a factorial with do while you can calculate it in the following way:

$numero = 10;
$factorial=1;
do{
   $factorial*=$numero;
   $numero--;
}while($numero>1);

echo $factorial;//devuelve 3628800

I hope you can adapt it to your needs, Regards.

EDIT

Now that your code is readable the problem you are having is that you never declare the value 1 for $i and additionally in the part of the while you lack the $ to the variable numero , you would be left of the following way:

<?php

    echo"<table border=1 cellspacing=0 width=200";
    echo "<tr><th colspan=5> Factorial </th></tr>";    
    $factorial=1;
    $numero=$_REQUEST['valor1'];
    $i=1; //<- corrección 
    do{    
        echo "<tr> <td align=center>$factorial</td>
        <td align=center>x</td>
        <td align=center>$i</td>
        <td align=center>=</td>
        <td align=center> ". $factorial*$i . "</td></tr>";

        $factorial=$factorial*$i;
        echo "<td> ".$factorial. " </td>";    
        $i++;      
    }while($i<=$numero); //<- corrección 

?>

Now if you want to avoid declaring the $i and not doing twice the multiplication $factorial*$i , it may look like this:

<?php

 echo"<table border=1 cellspacing=0 width=200";
    echo "<tr><th colspan=5> Factorial </th></tr>";    
    $factorial=1;
    $numero=$_REQUEST['valor1'];
    do{    
        echo "<tr> <td align=center>$factorial</td>";

        $factorial*=$numero;

        echo "<td align=center>x</td>
        <td align=center>$numero</td>
        <td align=center>=</td>
        <td align=center>".$factorial."</td></tr>
        <td> ".$factorial. " </td>";    

        $numero--;      
    }while($numero>1);  

?>

Greetings!

    
answered by 20.04.2018 / 22:00
source