October 18, 2020•5 min read

Arithmetic Operations in Pascal

Once you know what a data type is, the next question is which operations each type allows. Here is the answer for Pascal, type by type.

Read in Indonesian

Arithmetic operations are the ones we meet in math: add, subtract, multiply, divide, and plenty more. In Pascal we also use them to manipulate data. Programming isn't only about numbers, so the operations differ from one data type to the next.

Now that you know what a data type is, we'll try to learn which operations each type allows in Pascal. Most of them apply to other programming languages too.

Operations on the Integer Type in Pascal

The integer type holds negative whole numbers, positive ones, and zero. It knows no decimals and no fractions. So any operation on an integer must not produce a decimal.

The operations allowed on integers are add, subtract, multiply, divide, and modulus. The integer type has no division operation, because that could produce a decimal.

Operation Sign Description Example
Addition + Adds 2 whole numbers 2+2 = 4
Subtraction - Subtracts 2 whole numbers 6-4 = 2
Multiplication * Multiplies 2 whole numbers 6*6 = 36
Division div Divides 2 numbers, rounded down 5 div 2 = 2
Modulus mod Takes the remainder of 2 numbers 5 mod 2 = 1

Integer operations include 2 that don't exist in math: div and mod.

The div operation divides with rounding down. I prefer to call it division without a remainder. The mod operation divides but keeps only the remainder.

For example 9 : 2 = 4 remainder 1. So 9 div 2 = 4 and 9 mod 2 = 1.

Another example is 7 : 5 = 1 remainder 2. So 7 div 5 = 1, and 7 mod 5 = 2.

To understand div and mod better, type the code below and run it.

PASCAL
program OperasiDivMod;
uses crt;

// variable declaration
var
  a, b :integer;
  hasil :integer;
  sisa :integer;

{This program shows the quotient of a and b
in the form 'quotient and remainder' (not as a decimal)}
begin
  clrscr;

  // assign values to the variables
  a := 7;
  b := 5;
  hasil := a div b; // quotient of a and b, no remainder
  sisa := a mod b; // remainder of a and b

  // output
  writeln('nilai a = ', a); // show the value of a
  writeln('nilai b = ', b); // show the value of b

  // show the division operation
  write(a, ' / ', b, ' = '); // show a and b
  writeln(hasil, ' sisa ', sisa); // show quotient and remainder

  readln;
end.

The program output will look something like this.

TEXT
nilai a = 7
nilai b = 5
7 / 5 = 1 sisa 2

As an experiment you can change the values of a and b. Oh, and try swapping the div above for a division sign (/) and see what happens hehe.

Operations on the Real Type in Pascal

The next operation is on the Real type. Both Real and Integer hold numbers, so most of the operations are the same.

The difference is only in division. The Real type knows division, Integer doesn't. On the other hand, Real doesn't know div and mod.

There's one more thing for the real type, though strictly it's a format, not an operation. By default Pascal prints decimals with a pile of zeros after the point. You can fix that with real formatting.

Operation Sign Description Example
Addition + Adds 2 numbers 2.7+2.7 = 5.4
Subtraction - Subtracts 2 numbers 6.9-10.9 = -4.0
Multiplication * Multiplies 2 numbers 0.6*6 = 3.6
Division / Divides 2 numbers 5 / 2 = 2.5
Format r:[a]:b Formats a real 3.1415:2:2 = 3.14

In the table above, r:[a]:b means r: a real number; a: the number of digits before the point; b: the number of digits after the point.

In Pascal, whole numbers can also go into a Real variable, unlike Integer which only takes whole numbers. That's because a whole number can be written in decimal form: 7 (integer) can be written as 7.0 (real).

Let's code with the real type, to make it clearer hehe.

PASCAL
program OperasiReal;
uses crt;

// variable declaration
var
  a, b: integer;
  c, d: real;

begin
  clrscr;

  // assign values to the variables
  a := -20;  // negative number
  b := 4;
  c := a/b;
  d := -c/b; // negative divided by negative is positive

  // output variable values
  writeln('a = ', a);
  writeln('b = ', b);
  writeln('c = ', c);
  writeln('d = ', d);
  readln;
end.

If you run it, the output looks roughly like this.

TEXT
a = -20
b = 4
c =  -5.0000000000000000E+000
d =  1.2500000000000000E+000

Wow, why so many zeros there :o That's because the variables c and e aren't formatted yet. To format them, add a format operation to the writeln command. Now change your output lines for c and d to this.

PASCAL
....
  writeln('c = ', c:2:2);
  writeln('d = ', d:2:2);
....

Once that's done, the output turns into this.

TEXT
a = -20
b = 4
c = -5.00
d = 1.25

So the digits before and after the point are capped at 2. Much easier on the eyes now :D.

Operation Priority Order in Pascal

The priority order for operations in Pascal is the same as in math:

  1. Operations in parentheses come first
  2. Then multiplication, division, div, and mod
  3. Then addition and subtraction
  4. Operations always run from left to right

Say we have a variable a of type real. Then 2*3+7 is not the same as 2*(3+7)

Closing

Arithmetic operations in Pascal are really the same as in math. All of them will be useful for manipulating data later. Operations for other types, like string and boolean, come in later tutorials hehe.

That's all for this Pascal tutorial, hope it helps 👋 Next we'll cover input in Pascal.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.