Learn MODULA with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple Modula-2 Program
# modula2/demo.m2
MODULE Counter;
IMPORT InOut;
BEGIN
FOR i := 1 TO 5 DO
InOut.WriteInt(i, 0);
InOut.WriteLn;
END;
END Counter.
A simple Modula-2 program printing numbers 1 to 5.
2
Simple Modula-3 Program
# modula3/demo.m3
MODULE Counter;
IMPORT IO;
BEGIN
FOR i := 1 TO 5 DO
IO.Put(i);
IO.PutChar('\n');
END;
END Counter.
A simple Modula-3 program printing numbers 1 to 5.
3
Modula-2 Fibonacci Sequence
# modula2/fib.m2
MODULE Fibonacci;
IMPORT InOut;
VAR a, b, tmp, i: INTEGER;
BEGIN
a := 0; b := 1;
FOR i := 1 TO 10 DO
InOut.WriteInt(a, 0);
InOut.WriteLn;
tmp := a + b;
a := b;
b := tmp;
END;
END Fibonacci.
Calculates and prints Fibonacci numbers up to 10.
4
Modula-3 Factorial Program
# modula3/factorial.m3
MODULE Factorial;
IMPORT IO;
VAR n, fact, i: INTEGER;
BEGIN
n := 5; fact := 1;
FOR i := 1 TO n DO
fact := fact * i;
END;
IO.Put(fact);
IO.PutChar('\n');
END Factorial.
Calculates factorial of 5 and prints the result.
5
Modula-2 Array Sum
# modula2/array_sum.m2
MODULE ArraySum;
IMPORT InOut;
VAR arr: ARRAY 1..5 OF INTEGER := (1,2,3,4,5);
sum, i: INTEGER;
BEGIN
sum := 0;
FOR i := 1 TO 5 DO
sum := sum + arr[i];
END;
InOut.WriteInt(sum,0);
InOut.WriteLn;
END ArraySum.
Sums elements of an integer array.
6
Modula-3 While Loop Example
# modula3/while_loop.m3
MODULE WhileDemo;
IMPORT IO;
VAR i: INTEGER;
begin
i := 1;
WHILE i <= 5 DO
IO.Put(i);
IO.PutChar('\n');
i := i + 1;
END;
END WhileDemo.
Prints numbers from 1 to 5 using a WHILE loop.
7
Modula-2 Conditional Example
# modula2/if_example.m2
MODULE EvenOdd;
IMPORT InOut;
VAR n: INTEGER;
BEGIN
n := 7;
IF n MOD 2 = 0 THEN
InOut.WriteString("Even");
ELSE
InOut.WriteString("Odd");
END;
InOut.WriteLn;
END EvenOdd.
Checks if a number is even or odd.
8
Modula-3 Simple Function
# modula3/double.m3
MODULE DoubleFunc;
IMPORT IO;
PROCEDURE Double(x: INTEGER): INTEGER;
BEGIN
RETURN 2*x;
END Double;
BEGIN
IO.Put(Double(5));
IO.PutChar('\n');
END DoubleFunc.
Defines a function to double a number and prints the result.
9
Modula-2 Nested Loops
# modula2/mult_table.m2
MODULE Table;
IMPORT InOut;
VAR i,j: INTEGER;
BEGIN
FOR i := 1 TO 5 DO
FOR j := 1 TO 5 DO
InOut.WriteInt(i*j,0);
InOut.WriteString(" ");
END;
InOut.WriteLn;
END;
END Table.
Prints a 5x5 multiplication table.
10
Modula-3 Record Example
# modula3/record.m3
MODULE PointDemo;
IMPORT IO;
TYPE Point = RECORD x,y: INTEGER END;
VAR p: Point;
BEGIN
p.x := 10; p.y := 20;
IO.Put(p.x);
IO.PutChar(',');
IO.Put(p.y);
IO.PutChar('\n');
END PointDemo.
Defines a record type for a point and prints its coordinates.