PHP Online Compiler
Example: PHP Program to Find Roots of Quadratic Equation using sqrt() Function
C
C++
C#
Java
Python
PHP
main.php
STDIN
Run
<?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
Clear
ADVERTISEMENTS