PHP Program to Find Roots of Quadratic Equation using sqrt() Function
ADVERTISEMENTS
PHP program to find roots of quadratic equation using sqrt() function. In this article, you will learn how to find roots of quadratic equation in php using sqrt() function.
What is the Quadratic equation?
ax2 + bx + c = 0
There is a != 0 and, a, b, and c are the real numbers
There is a != 0 and, a, b, and c are the real numbers
Source Code
<?php
// PHP Program to Find Roots of Quadratic Equation using sqrt() Function
$p = 7.7;
$q = 6.6;
$r = 5.5;
$r1 = $r2 = $rp = $ip = 0;
// $p, $q, and $r - To store the real numbers
// $d - To store the discriminant
// $r1 - To store the first root
// $r2 - To store the second root
// $rp - To store the real part
// $ip - To store the image part
echo "----These are the values of the coefficients----\n";
echo $p, "\n", $q, "\n", $r, "\n";
$d = $q * $q - 4 * $p * $r;
// This is the condition for real and different roots
if ($d > 0) {
$r1 = (-$q + sqrt($d)) / (2 * $p);
$r2 = (-$q - sqrt($d)) / (2 * $p);
echo "\n\nThe r1 = ", $r1, " & r2 = ", $r2, "\n";
}
// This is the condition for real and equal roots
else if ($d == 0) {
$r1 = $r2 = -$q / (2 * $p);
echo "\n\nThe r1 = r2 = ", $r1, ";\n";
}
// if the roots are not real number
else {
$rp = -$q / (2 * $p);
$ip = sqrt(-$d) / (2 * $p);
echo "\n\nThe r1 = ", number_format($rp, 2), "+", number_format($ip, 2), "i & r2 = ", number_format($rp, 2), "-", number_format($ip, 2), "i\n";
}
?>
Output
----These are the values of the coefficients----
7.7
6.6
5.5
The r1 = -0.43+0.73i & r2 = -0.43-0.73i