Example of using arithmetic operators in Perl
This example shows how to use the arithmetic operators in Perl.
Code:
# Define some variables
my $num1 = 80;
my $num2 = 30;
my $num3 = 50;
# Print values
print '$num1 = 80' . "\n";
print '$num2 = 30' . "\n";
print '$num3 = 50' . "\n";
print "\n";
# Create a separator line, repeating to 25 times the character -
$sep = "-" x 25 . "\n";
# Operator < and >
print '$num1<$num2 : ' . "$num1<$num2 = ";
print $num1<$num2 ? "true" : "false";
print "\n";
print '$num2<$num3 : ' . "$num2<$num3 = ";
print $num2<$num3 ? "true" : "false";
print "\n";
print '$num3>$num1 : ' . "$num3>$num1 = ";
print $num3>$num1 ? "true" : "false";
print "\n";
print $sep;
# Operator +
print '$num1+$num2 : ' . "$num1+$num2=";
print $num1+$num2;
print "\n";
print '$num2+$num3 : ' . "$num2+$num3=";
print $num2+$num3;
print "\n";
print '$num3+$num1 : ' . "$num3+$num1=";
print $num3+$num1;
print "\n";
print $sep;
# Operator /
print '$num1/$num2 : ' . "$num1/$num2=";
$idiv = sprintf "%.3f", $num1/$num2;
print $idiv;
print "\n";
print '$num2/$num3 : ' . "$num2/$num3=";
$idiv = sprintf "%.3f", $num2/$num3;
print $idiv;
print "\n";
print '$num3/$num1 : ' . "$num3/$num1=";
$idiv = sprintf "%.3f", $num3/$num1;
print $idiv;
print "\n";
print $sep;
# Operator *
print '$num1*$num2 : ' . "$num1*$num2=";
print $num1*$num2;
print "\n";
print '$num2*$num3 : ' . "$num2*$num3=";
print $num2*$num3;
print "\n";
print '$num3*$num1 : ' . "$num3*$num1=";
print $num3*$num1;
print "\n";
print $sep;
# rand floating point
$nfp = 1 * (rand 90);
print '[1 * (rand 90)] = ' . $nfp;
print "\n";
# rand integer
$ni = int(1 * (rand 90));
print '[int(1 * (rand 90))] = ' . $ni;
print "\n";
print $sep;
# Extraction of six random integers
for($n=0;$n<7;$n++) {
print int(1 * (rand 90)) . "\n";
}
Sample result:
mdmsoft@linux-4nve:~/perl> perl operator.pl
$num1 = 80
$num2 = 30
$num3 = 50
$num1<$num2 : 80<30 = false
$num2<$num3 : 30<50 = true
$num3>$num1 : 50>80 = false
-------------------------
$num1+$num2 : 80+30=110
$num2+$num3 : 30+50=80
$num3+$num1 : 50+80=130
-------------------------
$num1/$num2 : 80/30=2.667
$num2/$num3 : 30/50=0.600
$num3/$num1 : 50/80=0.625
-------------------------
$num1*$num2 : 80*30=2400
$num2*$num3 : 30*50=1500
$num3*$num1 : 50*80=4000
-------------------------
[1 * (rand 90)] = 3.28490763168734
[int(1 * (rand 90))] = 49
-------------------------
51
13
48
77
66
36
67
If our work has been of help, you can help us with a small donation...
Our programmers will thank you!
Related code snippets posted in
Perl:
The best
Perl
recommended books:
All information contained in this web site are the property of MdmSoft.
The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages,
nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings.
MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only,
and may be copied only for your reference, but cannot be used for commercial purposes,
or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site,
the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.