123. PHP Program to Compute the Sine Series 1 - x^3/3! + x^5/5! – x^7/7! + …… x^n/n!
How does this program work?
- This program is used to find the Sum of the given Sine series 1 - x^3/3! + x^5/5! – x^7/7! + …… x^n/n! using PHP.
- The integer entered by the user is stored in variable x and n but x value in degrees.
- By using the for loop condition we can easily calculate the sum of sine series.
Here is the code
<html>
<head>
<title>Find the Sum of Sine Series</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter x value in degrees"/> </td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter n value"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$x = $_POST['num1'];
$n = $_POST['num2'];
$x1 = $x;
$pi = 3.14159; //pi = 22/7
$precision = 3;
$x = $x*$pi/180;
$sum = $x;
$t = $x;
// Loop to calculate the value of Sine
for($i=1;$i<=$n;$i++)
{
$t = ($t*(-1)*$x*$x)/(2*$i*(2*$i+1));
$sum = number_format(($sum + $t), $precision);
}
echo "X value : ".$x1."</br>";
echo "N value : ".$n ."</br>";
echo(" The value of Sin($x) is : " .$sum);
return 0;
}
?>
</body>
</html>