Learn ALGOL with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
ALGOL Counter and Theme Toggle
begin
integer count;
boolean isDark;
count := 0;
isDark := false;
procedure updateUI;
begin
print("Counter: ", count);
print("Theme: ", if isDark then "Dark" else "Light");
end;
procedure increment;
begin
count := count + 1;
updateUI;
end;
procedure decrement;
begin
count := count - 1;
updateUI;
end;
procedure reset;
begin
count := 0;
updateUI;
end;
procedure toggleTheme;
begin
isDark := not isDark;
updateUI;
end;
comment Simulate actions;
updateUI;
increment;
increment;
toggleTheme;
decrement;
reset;
end
Demonstrates a simple counter with theme toggling using ALGOL-style structured programming.
2
ALGOL Fibonacci Sequence
begin
integer a, b, c, i;
a := 0;
b := 1;
print(a);
print(b);
for i := 1 step 1 until 8 do
begin
c := a + b;
print(c);
a := b;
b := c;
end;
end
Generates the first 10 Fibonacci numbers.
3
ALGOL Factorial Calculator
begin
integer n, f, i;
n := 5;
f := 1;
for i := 1 step 1 until n do
begin
f := f * i;
end;
print(f);
end
Calculates the factorial of a number.
4
ALGOL Prime Checker
begin
integer num, i;
boolean isPrime;
num := 13;
isPrime := true;
for i := 2 step 1 until num-1 do
begin
if num mod i = 0 then
begin
isPrime := false;
goto end_loop;
end;
end;
end_loop:
if isPrime then print("Prime") else print("Not Prime");
end
Checks if a number is prime.
5
ALGOL Sum of Array
begin
integer arr[5], sum, i;
arr[1] := 1; arr[2] := 2; arr[3] := 3; arr[4] := 4; arr[5] := 5;
sum := 0;
for i := 1 step 1 until 5 do
sum := sum + arr[i];
print(sum);
end
Calculates the sum of an integer array.
6
ALGOL Reverse String
begin
string s, r;
integer i;
s := "Hello";
r := "";
for i := length(s) step -1 until 1 do
r := r + s[i];
print(r);
end
Reverses a string character by character.
7
ALGOL Multiplication Table
begin
integer n, i;
n := 5;
for i := 1 step 1 until 10 do
print(n, " x ", i, " = ", n*i);
end
Prints multiplication table of a number.
8
ALGOL Temperature Converter
begin
real c, f;
c := 25.0;
f := (c * 9.0 / 5.0) + 32.0;
print(f);
end
Converts Celsius to Fahrenheit.
9
ALGOL Simple Alarm Simulation
begin
integer temperature, threshold;
temperature := 80;
threshold := 75;
if temperature > threshold then
print("Alarm: Temperature Too High!")
else
print("Temperature Normal");
end
Simulates an alarm when a threshold is reached.
10
ALGOL Random Walk Simulation
begin
integer steps, i, position;
steps := 10;
position := 0;
for i := 1 step 1 until steps do
begin
if random(2) = 0 then
position := position + 1
else
position := position - 1;
print(position);
end;
end
Simulates a 1D random walk using integers.