Learn Modula2 - 10 Code Examples & CST Typing Practice Test
Modula-2 is a statically typed, modular, procedural programming language designed for systems programming and teaching structured programming concepts, created as a successor to Pascal.
Learn MODULA2 with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
Modula-2 Counter and Theme Toggle
MODULE CounterTheme;
VAR
count: INTEGER := 0;
isDark: BOOLEAN := FALSE;
PROCEDURE updateUI;
BEGIN
WriteString("Counter: "); WriteInt(count, 0); WriteLn;
IF isDark THEN
WriteString("Theme: Dark"); WriteLn
ELSE
WriteString("Theme: Light"); WriteLn;
END;
END updateUI;
PROCEDURE increment;
BEGIN
count := count + 1;
updateUI;
END increment;
PROCEDURE decrement;
BEGIN
count := count - 1;
updateUI;
END decrement;
PROCEDURE reset;
BEGIN
count := 0;
updateUI;
END reset;
PROCEDURE toggleTheme;
BEGIN
isDark := NOT isDark;
updateUI;
END toggleTheme;
BEGIN
updateUI;
increment;
increment;
toggleTheme;
decrement;
reset;
END CounterTheme.
Demonstrates a simple counter with theme toggling using Modula-2 variables and procedures.
Modula-2 Random Number Generator
MODULE RandomGen;
IMPORT InOut;
VAR i, num: INTEGER;
BEGIN
FOR i := 1 TO 3 DO
num := RANDOM(100) + 1;
InOut.WriteString("Random "); InOut.WriteInt(i,0); InOut.WriteString(": "); InOut.WriteInt(num,0); InOut.WriteLn;
END;
END RandomGen.
Generates random numbers between 1 and 100 and prints them.
Modula-2 Todo List
MODULE TodoList;
IMPORT InOut;
VAR todos: ARRAY 10 OF ARRAY 50 OF CHAR; count: INTEGER := 0;
PROCEDURE addTask(task: ARRAY OF CHAR);
BEGIN
todos[count] := task; count := count + 1;
FOR i := 0 TO count-1 DO InOut.WriteString(todos[i]); InOut.WriteLn END;
END addTask;
PROCEDURE removeTask(idx: INTEGER);
VAR i: INTEGER;
BEGIN
FOR i := idx TO count-2 DO todos[i] := todos[i+1] END;
count := count - 1;
FOR i := 0 TO count-1 DO InOut.WriteString(todos[i]); InOut.WriteLn END;
END removeTask;
BEGIN
addTask("Buy milk");
addTask("Write Modula-2 code");
removeTask(0);
END TodoList.
Maintains a simple todo list with add and remove functionality.
Modula-2 Dice Roller
MODULE DiceRoll;
IMPORT InOut;
VAR i, roll: INTEGER;
BEGIN
FOR i := 1 TO 3 DO
roll := RANDOM(6) + 1;
InOut.WriteString("Roll "); InOut.WriteInt(i,0); InOut.WriteString(": "); InOut.WriteInt(roll,0); InOut.WriteLn;
END;
END DiceRoll.
Rolls a six-sided dice three times.
Modula-2 Countdown Timer
MODULE Countdown;
IMPORT InOut;
VAR count: INTEGER;
BEGIN
count := 5;
WHILE count >= 0 DO
InOut.WriteString("Countdown: "); InOut.WriteInt(count,0); InOut.WriteLn;
count := count - 1;
END;
InOut.WriteString("Done!"); InOut.WriteLn;
END Countdown.
Counts down from 5 to 0.
Modula-2 Prime Checker
MODULE PrimeCheck;
IMPORT InOut;
VAR nums: ARRAY 3 OF INTEGER := [7,10,13];
VAR n, i: INTEGER;
VAR isPrime: BOOLEAN;
BEGIN
FOR n IN nums DO
isPrime := TRUE;
FOR i := 2 TO n-1 DO
IF n MOD i = 0 THEN isPrime := FALSE END;
END;
InOut.WriteInt(n,0);
IF isPrime THEN InOut.WriteString(" is Prime") ELSE InOut.WriteString(" is Not Prime") END;
InOut.WriteLn;
END;
END PrimeCheck.
Checks if numbers are prime.
Modula-2 Temperature Converter
MODULE TempConv;
IMPORT InOut;
VAR c,f: REAL;
PROCEDURE cToF(c: REAL): REAL;
BEGIN RETURN c*9/5+32 END cToF;
PROCEDURE fToC(f: REAL): REAL;
BEGIN RETURN (f-32)*5/9 END fToC;
BEGIN
InOut.WriteString("25°C = "); InOut.WriteReal(cToF(25),2); InOut.WriteLn;
InOut.WriteString("77°F = "); InOut.WriteReal(fToC(77),2); InOut.WriteLn;
END TempConv.
Converts Celsius to Fahrenheit and Fahrenheit to Celsius.
Modula-2 Shopping Cart
MODULE ShoppingCart;
IMPORT InOut;
VAR cart: ARRAY 10 OF ARRAY 50 OF CHAR; prices: ARRAY 10 OF INTEGER; count: INTEGER := 0;
PROCEDURE addItem(item: ARRAY OF CHAR; price: INTEGER);
BEGIN
cart[count] := item; prices[count] := price; count := count + 1;
(* print cart and total *)
END addItem;
PROCEDURE removeItem(idx: INTEGER);
VAR i: INTEGER;
BEGIN
FOR i := idx TO count-2 DO cart[i] := cart[i+1]; prices[i] := prices[i+1] END;
count := count -1;
(* print cart and total *)
END removeItem;
BEGIN
addItem("Apple", 2);
addItem("Banana", 3);
removeItem(0);
END ShoppingCart.
Adds and removes items in a shopping cart with total cost.
Modula-2 Name Greeting
MODULE Greet;
IMPORT InOut;
PROCEDURE greet(name: ARRAY OF CHAR);
BEGIN
InOut.WriteString("Hello, "); InOut.WriteString(name); InOut.WriteString("! Welcome!"); InOut.WriteLn;
END greet;
BEGIN
greet("Saurav");
greet("Alice");
greet("Bob");
END Greet.
Greets users by name.
Modula-2 Stopwatch
MODULE Stopwatch;
IMPORT InOut;
VAR time: INTEGER := 0;
BEGIN
WHILE time < 5 DO
InOut.WriteString("Stopwatch: "); InOut.WriteInt(time,0); InOut.WriteLn;
time := time + 1;
END;
InOut.WriteString("Done!"); InOut.WriteLn;
END Stopwatch.
Simulates a stopwatch incrementing seconds.
Frequently Asked Questions about Modula2
What is Modula2?
Modula-2 is a statically typed, modular, procedural programming language designed for systems programming and teaching structured programming concepts, created as a successor to Pascal.
What are the primary use cases for Modula2?
Teaching structured and modular programming. Systems programming and embedded applications. Operating system and compiler development. Prototyping modular software architectures. Applications requiring strong type safety
What are the strengths of Modula2?
Enforces disciplined programming with strong typing. Supports modular software design. Good for systems programming and low-level operations. Readable syntax similar to Pascal. Facilitates separate compilation and encapsulation
What are the limitations of Modula2?
Limited modern library ecosystem. Mostly academic or legacy use today. No native support for GUI programming. Not widely adopted in industry. Less flexible compared to modern object-oriented languages
How can I practice Modula2 typing speed?
CodeSpeedTest offers 10+ real Modula2 code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.