1

我正在准备期末考试,我遇到了这个问题:

write a PHP script which reads a positive integer
and displays the sum, the number, sum N*N and N!
for example n=6 will display sum= 1,3,6,10,15,21
and N*N = 1,4,9,16,25,36
N!=1,2,6,24,120,720.

我已经设法做到了,我已经研究过了,你可以使用内置的阶乘和求和方法,我已经尝试过,但是当我输出它时我得到了空白页。

到目前为止,这是我的代码:

<html>
   <body>
      <form action="values.php" method="post" >
         num:<input type="text" name="num" size ="5"/>
         <input type = "submit" value = "Submit number" />

      <table border = "2">
        <th> Number </th>
        <th> Sum </th>
        <th> N*N </th>
        <th> N! </th>

        </tr>
     <?php
        $num=$_POST["num"];
        if ($num==0)
            $num="";
        else
        {
           $sum=0;
           for($i=0; $i<=$num; $i++){
           $sum=$sum+$i;
        }
     }


          for ($number = 1; $number <=6; $number++)
          {
            $total=0;
            $num=(int)$_POST['num'];
            $total=$total+$num;

               $root = sqrt($number);
               $sum =($number*$total);
               $ntn =($number*($total*$total));
               $fact =($number-1);

               print("
               <tr align = 'center'>
               <td> $number </td>
               <td> $sum </td>
               <td>$ntn </td>
               <td>$fact</td>
               </tr>\n");
          }
    ?>
</table>


</body>

任何帮助,将不胜感激。

提前致谢!

4

1 回答 1

2

工作代码:

你似乎没有理解这个问题。“总和”列是“直到 i 的所有数字的总和”,其中 i 的范围从 0 到 $num。

N*N 表示“i 的平方”,最后一个表示“i 的阶乘”。

<html>
   <body>
      <form action="values.php" method="post" >
         num:<input type="text" name="num" size ="5"/>
         <input type = "submit" value = "Submit number" />

      <table border = "2">
        <th> Number </th>
        <th> Sum </th>
        <th> N*N </th>
        <th> N! </th>

        </tr>
     <?php
        $num=$_POST["num"];
        if ($num==0){
            $num="";
     }

$sum=0;
$fact=1;
          for ($number = 1; $number <=$num; $number++)
          {

           $sum=$sum+$number;
               $ntn =$number*$number;
               $fact =$number*$fact;

               print("
               <tr align = 'center'>
               <td> $number </td>
               <td> $sum </td>
               <td>$ntn </td>
               <td>$fact</td>
               </tr>\n");
          }
    ?>
</table>


</body>
于 2012-04-23T08:20:31.197 回答