Im studying for finals and i came across this question:
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.
i have managed to do the number that’s it, i have researched, you can use the inbuilt factorial and sum method, i have tried but i get empty pages when i output it.
here is my code so far:
<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>
Any help would be appreciated.
Thanks in advance!
Working code:
You don’t seem to have understood the question. The “sum” column was to have “sum of all numbers up to i”, where i ranged from 0 to $num.
N*N was to hold “squares of i”, and the last one held “factorial of i”.