-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadratic-equation.pl
More file actions
39 lines (32 loc) · 982 Bytes
/
quadratic-equation.pl
File metadata and controls
39 lines (32 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#------------------------------------------------------------------------------------------------------
# You run this script as follows:
#
# perl quadratic-equation.pl
#
# quadratic equation/second-degree equation
# ax² + bx + c = 0
# from a, b and c coeficients
#------------------------------------------------------------------------------------------------------
use strict;
use warnings;
print "Type a: "; #type from the keyboard
my $a = <>;
chomp $a;
print "Type b: "; #type from the keyboard
my $b = <>;
chomp $b;
print "Type c: "; #type from the keyboard
my $c = <>;
chomp $c;
my $res = (($b**2) - (4*$a*$c));
my ($x, $x1, $x2);
if ($res < 0){
print "There are no roots!\n";
}elsif ($res == 0){
$x = ((-$b) / (2*$a));
print "The root is $x.\n";
}elsif ($res > 0){
$x1 = ((-$b + sqrt($res)) / (2*$a));
$x2 = ((-$b - sqrt($res)) / (2*$a));
print "The roots are: $x1 and $x2.\n";
}