我正在学习决赛,我遇到了这个问题:
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>
任何帮助都将不胜感激。
提前致谢!
答案 0 :(得分:2)
工作代码:
你似乎没有理解这个问题。 “sum”列是“所有数字总和最多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>