Pascal - Arithmetic Operators



Following table shows all the arithmetic operators supported by Pascal. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
div Divides numerator by denominator B div A will give 2
mod Modulus Operator AND remainder of after an integer division B mod A will give 0

The following example illustrates the arithmetic operators −

program calculator;
var
a,b,c : integer;
d: real;

begin
   a:=21;
   b:=10;
   c := a + b;
   
   writeln(' Line 1 - Value of c is ', c );
   c := a - b;
   
   writeln('Line 2 - Value of c is ', c );
   c := a * b;
   
   writeln('Line 3 - Value of c is ', c );
   d := a / b;
   
   writeln('Line 4 - Value of d is ', d:3:2 );
   c := a mod b;
   
   writeln('Line 5 - Value of c is ' , c );
   c := a div b;
   
      writeln('Line 6 - Value of c is ', c );
end.

Please note that Pascal is very strongly typed programming language, so it would give an error if you try to store the results of a division in an integer type variable. When the above code is compiled and executed, it produces the following result:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of d is 2.10
Line 5 - Value of c is 1
Line 6 - Value of c is 2
pascal_operators.htm
Advertisements