Quadratic equation in Perl
Example for versions
perl 5.12.1,
perl 5.8.8
Note that Perl 6 has no backwards compatibility; this example won’t be valid in Perl 6.
$A = <>;
if ($A == 0) {
print "Not a quadratic equation.";
}
else {
$B = <>;
$C = <>;
$D = $B * $B - 4 * $A * $C;
if ($D == 0) {
print "x = ", -0.5 * $B / $A;
}
else {
if ($D > 0) {
print "x1 = ", 0.5*(-$B + sqrt($D))/$A, "\nx2 = ", 0.5*(-$B - sqrt($D))/$A
}
else {
print "x1 = (", -0.5*$B/$A, ",", 0.5*sqrt(-$D)/$A, ")\nx2 = (", -0.5*$B/$A, ",", -0.5*sqrt(-$D)/$A, ")\n"
}
}
}
Comments
]]>blog comments powered by Disqus
]]>