Perl Tutorial Perl References

Perl - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), modulo(%), and exponent(**) operators.

$a = 9.0;
$b = 2.0;

print("a = $a, b = $b \n\n");

#Add a and b
$result_add = $a + $b;
print("a + b = $result_add\n");

#Subtract b from a
$result_sub = $a - $b;
print("a - b = $result_sub\n");

#Multiply a and b
$result_mul = $a * $b;
print("a * b = $result_mul\n");

#Divide a by b
$result_div = $a / $b;
print("a / b = $result_div\n");

#return division remainder
$result_modulo = $a % $b;
print("a % b = $result_modulo\n");

#a raised to the power of b
$result_pow = $a ** $b;
print("a ** b = $result_pow\n");

The output of the above code will be:

a = 9, b = 2 

a + b = 11
a - b = 7
a * b = 18
a / b = 4.5
a % b = 1
a ** b = 81

❮ Perl - Operators