我正在准备期末考试,我遇到了这个问题:
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>
任何帮助,将不胜感激。
提前致谢!