Quadratic equation in Perl
Example for versions
rakudo-2010.08
This example differs from one in Perl 5 in two major ways: the way user input is read and results are printed, and the way all variables have to be predeclared with my
keyword before using them. Note that explicit declaration of variable type stays optional.
my $A = $*IN.get;
if ($A == 0) {
say "Not a quadratic equation.";
}
else {
my $B = $*IN.get;
my $C = $*IN.get;
my $D = $B * $B - 4 * $A * $C;
if ($D == 0) {
say "x = ", -0.5 * $B / $A;
}
else {
if ($D > 0) {
say "x1 = ", 0.5*(-$B + sqrt($D))/$A, "\nx2 = ", 0.5*(-$B - sqrt($D))/$A
}
else {
say "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
]]>