AlgoML
FR

Example programs

Short, complete programs. Each one is checked: we generate the code for every language, run it and compare the output.

Get all the examples

Clone the repository and open the folder in VS Code.

git clone https://github.com/scmc24/algoml-examples.git

Basics

average.aml

Average of three marks.
Algorithm Average
version '1.0'
language C
variable m1 : REAL
variable m2 : REAL
variable m3 : REAL
variable mean : REAL
begin
read(m1)
read(m2)
read(m3)
mean <- (m1 + m2 + m3) / 3.0
write("Average: ", mean, "\n")
end
end algorithm
Input 12.0 15.5 9.5Output Average: 12.333333333333334average.aml on GitHubTry itSoon

circle.aml

Area of a circle with a constant PI.Explained in Types and variables
Algorithm Circle
version '1.0'
language C
constant PI : REAL = 3.14159
variable radius : REAL
variable area : REAL
begin
radius <- 2.0
area <- PI * radius * radius
write("Area: ", area, "\n")
end
end algorithm
Output Area: 12.56636circle.aml on GitHubTry itSoon

duration.aml

Split seconds into hours, minutes and seconds.
Algorithm Duration
version '1.0'
language C
variable total : INT
variable h : INT
variable m : INT
variable s : INT
begin
read(total)
h <- total div 3600
m <- total mod 3600 div 60
s <- total mod 60
write(total, " seconds = ", h, " h ", m, " min ", s, " s\n")
end
end algorithm
Input 7384Output 7384 seconds = 2 h 3 min 4 sduration.aml on GitHubTry itSoon

greeting.aml

Read a name and an age, then answer.Explained in Your first program
Algorithm Greeting
version '1.0'
language C
variable name : STRING
variable age : INT
begin
write("What is your name? ")
read(name)
write("How old are you? ")
read(age)
write("Hello ", name, ", next year you will be ", age + 1, ".\n")
end
end algorithm
Input Ada 36Output What is your name? How old are you? Hello Ada, next year you will be 37.greeting.aml on GitHubTry itSoon

hello.aml

The smallest program: display a message.Explained in Your first program
Algorithm Hello
version '1.0'
language C
begin
write("Hello, world!\n")
end
end algorithm
Output Hello, world!hello.aml on GitHubTry itSoon

minutes.aml

Convert minutes into hours and minutes.Explained in Your first program
Algorithm Minutes
version '1.0'
language C
variable total : INT
variable hours : INT
variable minutes : INT
begin
read(total)
hours <- total div 60
minutes <- total mod 60
write(total, " minutes = ", hours, " h ", minutes, " min\n")
end
end algorithm
Input 135Output 135 minutes = 2 h 15 minminutes.aml on GitHubTry itSoon

swap.aml

Swap two variables with a third one.
Algorithm Swap
version '1.0'
language C
variable a : INT
variable b : INT
variable tmp : INT
begin
a <- 4
b <- 9
write("Before: a = ", a, ", b = ", b, "\n")
tmp <- a
a <- b
b <- tmp
write("After: a = ", a, ", b = ", b, "\n")
end
end algorithm
Output Before: a = 4, b = 9 After: a = 9, b = 4swap.aml on GitHubTry itSoon

temperature.aml

Convert Celsius to Fahrenheit.
Algorithm Temperature
version '1.0'
language C
variable celsius : REAL
variable fahrenheit : REAL
begin
read(celsius)
fahrenheit <- celsius * 9.0 / 5.0 + 32.0
write(celsius, " C = ", fahrenheit, " F\n")
end
end algorithm
Input 37.5Output 37.5 C = 99.5 Ftemperature.aml on GitHubTry itSoon

Conditions

calculator.aml

Four operations chosen from a menu.
Algorithm Calculator
version '1.0'
language C
variable a : INT
variable b : INT
variable choice : INT
begin
read(a)
read(b)
read(choice)
switch (choice) :
case 1 :
write(a, " + ", b, " = ", a + b, "\n")
case 2 :
write(a, " - ", b, " = ", a - b, "\n")
case 3 :
write(a, " x ", b, " = ", a * b, "\n")
case 4 :
if b = 0 then
write("Division by zero\n")
else
write(a, " / ", b, " = ", a / b, "\n")
end if
default :
write("Unknown operation\n")
end switch
end
end algorithm
Input 7 2 4Output 7 / 2 = 3.5calculator.aml on GitHubTry itSoon

even_or_odd.aml

Is a number even or odd?
Algorithm EvenOrOdd
version '1.0'
language C
variable n : INT
begin
read(n)
if n mod 2 = 0 then
write(n, " is even\n")
else
write(n, " is odd\n")
end if
end
end algorithm
Input 27Output 27 is oddeven_or_odd.aml on GitHubTry itSoon

grade.aml

Turn a mark into a grade with elseif.Explained in Conditions
Algorithm Grade
version '1.0'
language C
variable mark : INT
begin
read(mark)
if mark >= 16 then
write("Very good\n")
elseif mark >= 12 then
write("Good\n")
elseif mark >= 10 then
write("Pass\n")
else
write("Fail\n")
end if
end
end algorithm
Input 13Output Goodgrade.aml on GitHubTry itSoon

leap_year.aml

Is a year a leap year?
Algorithm LeapYear
version '1.0'
language C
variable year : INT
begin
read(year)
if (year mod 4 = 0 AND year mod 100 <> 0) OR year mod 400 = 0 then
write(year, " is a leap year\n")
else
write(year, " is not a leap year\n")
end if
end
end algorithm
Input 2024Output 2024 is a leap yearleap_year.aml on GitHubTry itSoon

max_of_three.aml

Largest of three numbers.Explained in Conditions
Algorithm MaxOfThree
version '1.0'
language C
variable a : INT
variable b : INT
variable c : INT
variable max : INT
begin
read(a)
read(b)
read(c)
max <- a
if b > max then
max <- b
end if
if c > max then
max <- c
end if
write("The largest is ", max, "\n")
end
end algorithm
Input 7 19 4Output The largest is 19max_of_three.aml on GitHubTry itSoon

operators.aml

div, mod, AND, NOT and compound assignment.Explained in Expressions and operators
Algorithm Operators
version '1.0'
language C
variable a : INT
variable b : INT
variable even : BOOLEAN
begin
a <- 17
b <- 5
write(a div b, "\n")
write(a mod b, "\n")
even <- a mod 2 = 0
if NOT even AND a > b then
write("a is odd and greater than b\n")
end if
a +<- 3
write(a, "\n")
end
end algorithm
Output 3 2 a is odd and greater than b 20operators.aml on GitHubTry itSoon

sign.aml

Positive, negative or zero.
Algorithm Sign
version '1.0'
language C
variable x : INT
begin
read(x)
if x > 0 then
write("positive\n")
elseif x < 0 then
write("negative\n")
else
write("zero\n")
end if
end
end algorithm
Input -8Output negativesign.aml on GitHubTry itSoon

triangle.aml

Type of a triangle from its three sides.
Algorithm Triangle
version '1.0'
language C
variable a : INT
variable b : INT
variable c : INT
begin
read(a)
read(b)
read(c)
if a + b <= c OR a + c <= b OR b + c <= a then
write("Not a triangle\n")
elseif a = b AND b = c then
write("Equilateral\n")
elseif a = b OR b = c OR a = c then
write("Isosceles\n")
else
write("Scalene\n")
end if
end
end algorithm
Input 5 5 8Output Isoscelestriangle.aml on GitHubTry itSoon

weekday.aml

Name of the day with switch.Explained in Conditions
Algorithm Weekday
version '1.0'
language C
variable day : INT
begin
read(day)
switch (day) :
case 6 :
write("Saturday: weekend\n")
case 7 :
write("Sunday: weekend\n")
default :
write("Weekday\n")
end switch
end
end algorithm
Input 7Output Sunday: weekendweekday.aml on GitHubTry itSoon

Loops

collatz.aml

Steps of the Collatz sequence down to 1.
Algorithm Collatz
version '1.0'
language C
variable n : INT
variable steps : INT
begin
read(n)
steps <- 0
while n <> 1 do
if n mod 2 = 0 then
n <- n div 2
else
n <- 3 * n + 1
end if
steps <- steps + 1
end while
write("Reached 1 in ", steps, " steps\n")
end
end algorithm
Input 27Output Reached 1 in 111 stepscollatz.aml on GitHubTry itSoon

countdown.aml

Count down with while.
Algorithm Countdown
version '1.0'
language C
variable n : INT
begin
n <- 5
while n > 0 do
write(n, "\n")
n <- n - 1
end while
write("Go!\n")
end
end algorithm
Output 5 4 3 2 1 Go!countdown.aml on GitHubTry itSoon

digit_sum.aml

Sum of the digits of a number.
Algorithm DigitSum
version '1.0'
language C
variable n : INT
variable total : INT
begin
read(n)
total <- 0
while n > 0 do
total <- total + n mod 10
n <- n div 10
end while
write("Sum of the digits: ", total, "\n")
end
end algorithm
Input 90517Output Sum of the digits: 22digit_sum.aml on GitHubTry itSoon

even_numbers.aml

Even numbers from 2 to 20.Explained in Loops
Algorithm EvenNumbers
version '1.0'
language C
variable i : INT
begin
for i in 1 to 10 do
write(2 * i, "\n")
end for
end
end algorithm
Output 2 4 6 8 10 12 14 16 18 20even_numbers.aml on GitHubTry itSoon

loops.aml

for loops, with and without step.Explained in Loops
Algorithm Loops
version '1.0'
language C
variable i : INT
variable n : INT
begin
n <- 3
for i in 1 to n do
write(i, "\n")
end for
for i in 1 to 9 step 2 do
write(i, "\n")
end for
end
end algorithm
Output 1 2 3 1 3 5 7 9loops.aml on GitHubTry itSoon

password.aml

Ask for a password until it is right.
Algorithm Password
version '1.0'
language C
variable word : STRING
variable tries : INT
begin
tries <- 0
repeat
write("Password: ")
read(word)
tries <- tries + 1
until word = "algoml"
write("Welcome! ", tries, " tries\n")
end
end algorithm
Input hello AlgoML algomlOutput Password: Password: Password: Welcome! 3 triespassword.aml on GitHubTry itSoon

positive.aml

Ask again until the value is valid.Explained in Loops
Algorithm Positive
version '1.0'
language C
variable n : INT
begin
repeat
write("Enter a positive number: ")
read(n)
until n > 0
write("Thank you, you entered ", n, "\n")
end
end algorithm
Input -4 0 9Output Enter a positive number: Enter a positive number: Enter a positive number: Thank you, you entered 9positive.aml on GitHubTry itSoon

power.aml

Power of a number with a loop.
Algorithm Power
version '1.0'
language C
variable base : INT
variable exponent : INT
variable result : INT
variable i : INT
begin
read(base)
read(exponent)
result <- 1
for i in 1 to exponent do
result <- result * base
end for
write(base, "^", exponent, " = ", result, "\n")
end
end algorithm
Input 3 5Output 3^5 = 243power.aml on GitHubTry itSoon

reverse_number.aml

Write a number backwards.
Algorithm ReverseNumber
version '1.0'
language C
variable n : INT
variable reversed : INT
begin
read(n)
reversed <- 0
while n > 0 do
reversed <- reversed * 10 + n mod 10
n <- n div 10
end while
write(reversed, "\n")
end
end algorithm
Input 12345Output 54321reverse_number.aml on GitHubTry itSoon

stars.aml

A triangle of stars with nested loops.
Algorithm Stars
version '1.0'
language C
variable row : INT
variable col : INT
begin
for row in 1 to 5 do
for col in 1 to row do
write("*")
end for
write("\n")
end for
end
end algorithm
Output * ** *** **** *****stars.aml on GitHubTry itSoon

sum_to_n.aml

Sum of the numbers from 1 to n.
Algorithm SumToN
version '1.0'
language C
variable n : INT
variable i : INT
variable total : INT
begin
read(n)
total <- 0
for i in 1 to n do
total <- total + i
end for
write("1 + 2 + ... + ", n, " = ", total, "\n")
end
end algorithm
Input 100Output 1 + 2 + ... + 100 = 5050sum_to_n.aml on GitHubTry itSoon

Functions and procedures

absolute.aml

Absolute value of a REAL.
Algorithm Absolute
version '1.0'
language C
function absolute(x : REAL) : REAL
begin
if x < 0.0 then
return -x
end if
return x
end function
begin
write(absolute(-3.5), "\n")
write(absolute(2.25), "\n")
end
end algorithm

factorial.aml

Factorial with a recursive function.Explained in Introduction
Algorithm Factorial
version '1.0'
language C
function factorial(n : INT) : INT
begin
if n <= 1 then
return 1
end if
return n * factorial(n - 1)
end function
variable n : INT
variable result : INT
begin
read(n)
result <- factorial(n)
write(n, "! = ", result, "\n")
end
end algorithm
Input 5Output 5! = 120factorial.aml on GitHubTry itSoon

fibonacci.aml

The first ten Fibonacci numbers.
Algorithm Fibonacci
version '1.0'
language C
function fib(n : INT) : INT
variable a : INT
variable b : INT
variable tmp : INT
variable i : INT
begin
a <- 0
b <- 1
for i in 1 to n do
tmp <- a + b
a <- b
b <- tmp
end for
return a
end function
variable i : INT
begin
for i in 1 to 10 do
write(fib(i), "\n")
end for
end
end algorithm
Output 1 1 2 3 5 8 13 21 34 55fibonacci.aml on GitHubTry itSoon

gcd.aml

Greatest common divisor, Euclid's method.Explained in Program structure
Algorithm Gcd
version '1.0'
language C
function gcd(a : INT, b : INT) : INT
variable r : INT
begin
while b <> 0 do
r <- a mod b
a <- b
b <- r
end while
return a
end function
variable x : INT
variable y : INT
begin
read(x)
read(y)
write("gcd = ", gcd(x, y), "\n")
end
end algorithm
Input 48 36Output gcd = 12gcd.aml on GitHubTry itSoon

hanoi.aml

Towers of Hanoi with a recursive procedure.
Algorithm Hanoi
version '1.0'
language C
procedure move(n : INT, source : STRING, target : STRING, spare : STRING)
begin
if n > 0 then
move(n - 1, source, spare, target)
write("Move disk ", n, " from ", source, " to ", target, "\n")
move(n - 1, spare, target, source)
end if
end procedure
begin
move(3, "A", "C", "B")
end
end algorithm
Output Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to Chanoi.aml on GitHubTry itSoon

is_prime.aml

Prime numbers up to 20 with a BOOLEAN function.Explained in Functions and procedures
Algorithm IsPrime
version '1.0'
language C
function isPrime(n : INT) : BOOLEAN
variable d : INT
begin
if n < 2 then
return False
end if
d <- 2
while d * d <= n do
if n mod d = 0 then
return False
end if
d <- d + 1
end while
return True
end function
variable n : INT
begin
for n in 1 to 20 do
if isPrime(n) then
write(n, "\n")
end if
end for
end
end algorithm
Output 2 3 5 7 11 13 17 19is_prime.aml on GitHubTry itSoon

lcm.aml

Least common multiple, using gcd.
Algorithm Lcm
version '1.0'
language C
function gcd(a : INT, b : INT) : INT
variable r : INT
begin
while b <> 0 do
r <- a mod b
a <- b
b <- r
end while
return a
end function
function lcm(a : INT, b : INT) : INT
begin
return a div gcd(a, b) * b
end function
begin
write("lcm(12, 18) = ", lcm(12, 18), "\n")
end
end algorithm
Output lcm(12, 18) = 36lcm.aml on GitHubTry itSoon

min_max.aml

Two small functions: minimum and maximum.
Algorithm MinMax
version '1.0'
language C
function minimum(a : INT, b : INT) : INT
begin
if a < b then
return a
end if
return b
end function
function maximum(a : INT, b : INT) : INT
begin
if a > b then
return a
end if
return b
end function
begin
write("min = ", minimum(14, 9), ", max = ", maximum(14, 9), "\n")
end
end algorithm
Output min = 9, max = 14min_max.aml on GitHubTry itSoon

power_recursive.aml

Power of a number, recursive version.
Algorithm PowerRecursive
version '1.0'
language C
function power(x : INT, n : INT) : INT
begin
if n = 0 then
return 1
end if
return x * power(x, n - 1)
end function
begin
write("2^10 = ", power(2, 10), "\n")
end
end algorithm

table.aml

Multiplication table in a procedure.Explained in Functions and procedures
Algorithm Table
version '1.0'
language C
procedure printTable(n : INT)
variable i : INT
begin
for i in 1 to 5 do
write(n, " x ", i, " = ", n * i, "\n")
end for
end procedure
begin
printTable(7)
end
end algorithm
Output 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35table.aml on GitHubTry itSoon

Arrays

count_occurrences.aml

How many times a value appears.
Algorithm CountOccurrences
version '1.0'
language C
variable t : ARRAY[10] of INT
variable i : INT
variable target : INT
variable count : INT
begin
t <- [3, 7, 3, 1, 9, 3, 7, 2, 3, 8]
read(target)
count <- 0
for i in 1 to 10 do
if t[i] = target then
count <- count + 1
end if
end for
write(target, " appears ", count, " times\n")
end
end algorithm
Input 3Output 3 appears 4 timescount_occurrences.aml on GitHubTry itSoon

max_in_array.aml

Largest value of an array.Explained in Arrays
Algorithm MaxInArray
version '1.0'
language C
variable values : ARRAY[6] of INT
variable i : INT
variable max : INT
begin
values <- [14, 3, 27, 8, 19, 5]
max <- values[1]
for i in 1 to 6 do
if values[i] > max then
max <- values[i]
end if
end for
write("Max: ", max, "\n")
end
end algorithm

read_array.aml

Fill an array from the keyboard.
Algorithm ReadArray
version '1.0'
language C
variable values : ARRAY[5] of INT
variable x : INT
variable i : INT
variable total : INT
begin
for i in 1 to 5 do
read(x)
values[i] <- x
end for
total <- 0
for i in 1 to 5 do
total <- total + values[i]
end for
write("Total: ", total, "\n")
end
end algorithm
Input 4 8 15 16 23Output Total: 66read_array.aml on GitHubTry itSoon

reverse_array.aml

Reverse an array in place.
Algorithm ReverseArray
version '1.0'
language C
variable t : ARRAY[6] of INT
variable i : INT
variable tmp : INT
begin
t <- [1, 2, 3, 4, 5, 6]
for i in 1 to 3 do
tmp <- t[i]
t[i] <- t[7 - i]
t[7 - i] <- tmp
end for
for i in 1 to 6 do
write(t[i], "\n")
end for
end
end algorithm

second_largest.aml

Largest and second largest value.
Algorithm SecondLargest
version '1.0'
language C
variable t : ARRAY[7] of INT
variable i : INT
variable first : INT
variable second : INT
begin
t <- [12, 45, 7, 45, 30, 18, 3]
first <- t[1]
second <- 0
for i in 2 to 7 do
if t[i] > first then
second <- first
first <- t[i]
elseif t[i] < first AND t[i] > second then
second <- t[i]
end if
end for
write("Largest: ", first, ", second: ", second, "\n")
end
end algorithm
Output Largest: 45, second: 30second_largest.aml on GitHubTry itSoon

shift_left.aml

Rotate an array one cell to the left.
Algorithm ShiftLeft
version '1.0'
language C
variable t : ARRAY[5] of INT
variable i : INT
variable first : INT
begin
t <- [10, 20, 30, 40, 50]
first <- t[1]
for i in 1 to 4 do
t[i] <- t[i + 1]
end for
t[5] <- first
for i in 1 to 5 do
write(t[i], "\n")
end for
end
end algorithm
Output 20 30 40 50 10shift_left.aml on GitHubTry itSoon

sum_and_mean.aml

Sum and mean of the values of an array.Explained in Arrays
Algorithm SumAndMean
version '1.0'
language C
variable marks : ARRAY[5] of INT
variable i : INT
variable total : INT
begin
marks <- [12, 15, 9, 18, 11]
total <- 0
for i in 1 to 5 do
total <- total + marks[i]
end for
write("Sum: ", total, "\n")
write("Mean: ", total div 5, "\n")
end
end algorithm
Output Sum: 65 Mean: 13sum_and_mean.aml on GitHubTry itSoon

Structures

bank_account.aml

A bank account with a REAL balance.
Algorithm BankAccount
version '1.0'
language C
structure Account
owner : STRING
balance : REAL
end structure
variable acc : structure Account
begin
acc.owner <- "Ada"
acc.balance <- 100.0
acc.balance <- acc.balance + 50.5
acc.balance <- acc.balance - 20.0
write(acc.owner, " has ", acc.balance, " on her account\n")
end
end algorithm
Output Ada has 130.5 on her accountbank_account.aml on GitHubTry itSoon

date.aml

Which of two dates comes first.
Algorithm DateCheck
version '1.0'
language C
structure Date
day : INT
month : INT
year : INT
end structure
function before(d1 : structure Date, d2 : structure Date) : INT
begin
if d1.year <> d2.year then
if d1.year < d2.year then
return 1
end if
return 0
end if
if d1.month <> d2.month then
if d1.month < d2.month then
return 1
end if
return 0
end if
if d1.day < d2.day then
return 1
end if
return 0
end function
variable a : structure Date
variable b : structure Date
begin
a.day <- 14
a.month <- 7
a.year <- 2025
b.day <- 2
b.month <- 3
b.year <- 2026
if before(a, b) = 1 then
write("The first date comes first\n")
else
write("The second date comes first\n")
end if
end
end algorithm
Output The first date comes firstdate.aml on GitHubTry itSoon

point.aml

Squared distance between two points.
Algorithm PointDistance
version '1.0'
language C
structure Point
x : INT
y : INT
end structure
function distanceSquared(p : structure Point, q : structure Point) : INT
begin
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y)
end function
variable a : structure Point
variable b : structure Point
begin
a.x <- 1
a.y <- 2
b.x <- 4
b.y <- 6
write("Distance squared: ", distanceSquared(a, b), "\n")
end
end algorithm
Output Distance squared: 25point.aml on GitHubTry itSoon

rectangle.aml

A structure passed to a function.Explained in Structures and enums
Algorithm RectangleArea
version '1.0'
language C
structure Rectangle
width : INT
height : INT
end structure
function area(r : structure Rectangle) : INT
begin
return r.width * r.height
end function
variable r : structure Rectangle
begin
r.width <- 8
r.height <- 5
write("Area: ", area(r), "\n")
end
end algorithm

student.aml

A structure with a name and a mark.Explained in Structures and enums
Algorithm StudentCard
version '1.0'
language C
structure Student
name : STRING
mark : INT
end structure
variable s : structure Student
begin
s.name <- "Ada"
s.mark <- 15
write(s.name, " got ", s.mark, "/20\n")
end
end algorithm
Output Ada got 15/20student.aml on GitHubTry itSoon

Sorting and searching

bubble_sort.aml

Bubble sort.Explained in Arrays
Algorithm BubbleSort
version '1.0'
language C
variable tab : ARRAY[6] of INT
variable i : INT
variable j : INT
variable tmp : INT
begin
tab <- [5, 2, 9, 1, 7, 3]
for i in 1 to 5 do
for j in 1 to 6 - i do
if tab[j] > tab[j + 1] then
tmp <- tab[j]
tab[j] <- tab[j + 1]
tab[j + 1] <- tmp
end if
end for
end for
for i in 1 to 6 do
write(tab[i], "\n")
end for
end
end algorithm

insertion_sort.aml

Insertion sort.
Algorithm InsertionSort
version '1.0'
language C
variable t : ARRAY[6] of INT
variable i : INT
variable j : INT
variable key : INT
begin
t <- [31, 41, 59, 26, 41, 58]
for i in 2 to 6 do
key <- t[i]
j <- i - 1
while j >= 1 AND t[j] > key do
t[j + 1] <- t[j]
j <- j - 1
end while
t[j + 1] <- key
end for
for i in 1 to 6 do
write(t[i], "\n")
end for
end
end algorithm
Output 26 31 41 41 58 59insertion_sort.aml on GitHubTry itSoon

selection_sort.aml

Selection sort.
Algorithm SelectionSort
version '1.0'
language C
variable t : ARRAY[7] of INT
variable i : INT
variable j : INT
variable m : INT
variable tmp : INT
begin
t <- [29, 10, 14, 37, 13, 5, 21]
for i in 1 to 6 do
m <- i
for j in i + 1 to 7 do
if t[j] < t[m] then
m <- j
end if
end for
tmp <- t[i]
t[i] <- t[m]
t[m] <- tmp
end for
for i in 1 to 7 do
write(t[i], "\n")
end for
end
end algorithm
Output 5 10 13 14 21 29 37selection_sort.aml on GitHubTry itSoon

Number problems

armstrong.aml

Three-digit Armstrong numbers.
Algorithm Armstrong
version '1.0'
language C
variable n : INT
variable x : INT
variable digit : INT
variable total : INT
begin
for n in 100 to 999 do
x <- n
total <- 0
while x > 0 do
digit <- x mod 10
total <- total + digit * digit * digit
x <- x div 10
end while
if total = n then
write(n, "\n")
end if
end for
end
end algorithm
Output 153 370 371 407armstrong.aml on GitHubTry itSoon

divisors.aml

All the divisors of a number.
Algorithm Divisors
version '1.0'
language C
variable n : INT
variable d : INT
begin
read(n)
for d in 1 to n do
if n mod d = 0 then
write(d, "\n")
end if
end for
end
end algorithm
Input 36Output 1 2 3 4 6 9 12 18 36divisors.aml on GitHubTry itSoon

pascal.aml

Pascal's triangle, one array updated row by row.
Algorithm Pascal
version '1.0'
language C
variable row : ARRAY[7] of INT
variable n : INT
variable k : INT
begin
for n in 1 to 7 do
row[n] <- 1
k <- n - 1
while k >= 2 do
row[k] <- row[k] + row[k - 1]
k <- k - 1
end while
for k in 1 to n do
write(row[k], " ")
end for
write("\n")
end for
end
end algorithm
Output 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1pascal.aml on GitHubTry itSoon

perfect_numbers.aml

Perfect numbers below 1000.
Algorithm PerfectNumbers
version '1.0'
language C
function divisorSum(n : INT) : INT
variable d : INT
variable total : INT
begin
total <- 0
for d in 1 to n - 1 do
if n mod d = 0 then
total <- total + d
end if
end for
return total
end function
variable n : INT
begin
for n in 2 to 1000 do
if divisorSum(n) = n then
write(n, "\n")
end if
end for
end
end algorithm

prime_factors.aml

Prime factorisation.
Algorithm PrimeFactors
version '1.0'
language C
variable n : INT
variable d : INT
begin
read(n)
d <- 2
while n > 1 do
while n mod d = 0 do
write(d, "\n")
n <- n div d
end while
d <- d + 1
end while
end
end algorithm
Input 360Output 2 2 2 3 3 5prime_factors.aml on GitHubTry itSoon

sieve.aml

Sieve of Eratosthenes up to 50.
Algorithm Sieve
version '1.0'
language C
variable crossed : ARRAY[50] of INT
variable i : INT
variable j : INT
begin
for i in 1 to 50 do
crossed[i] <- 0
end for
for i in 2 to 50 do
if crossed[i] = 0 then
write(i, "\n")
j <- i * i
while j <= 50 do
crossed[j] <- 1
j <- j + i
end while
end if
end for
end
end algorithm
Output 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47sieve.aml on GitHubTry itSoon