AlgoML
EN

Programmes d'exemple

Des programmes courts et complets. Chacun est vérifié : on génère le code dans chaque langage, on l'exécute et on compare la sortie.

Récupérer tous les exemples

Clonez le dépôt et ouvrez le dossier dans VS Code.

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

Bases

average.aml

Moyenne de trois notes.
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
Entrée 12.0 15.5 9.5Sortie Average: 12.333333333333334average.aml sur GitHubEssayerBientôt

circle.aml

Aire d'un cercle avec une constante PI.Expliqué dans Types et 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
Sortie Area: 12.56636circle.aml sur GitHubEssayerBientôt

duration.aml

Découper des secondes en heures, minutes et secondes.
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
Entrée 7384Sortie 7384 seconds = 2 h 3 min 4 sduration.aml sur GitHubEssayerBientôt

greeting.aml

Lire un nom et un âge, puis répondre.Expliqué dans Votre premier programme
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
Entrée Ada 36Sortie What is your name? How old are you? Hello Ada, next year you will be 37.greeting.aml sur GitHubEssayerBientôt

hello.aml

Le plus petit programme : afficher un message.Expliqué dans Votre premier programme
Algorithm Hello
version '1.0'
language C
begin
write("Hello, world!\n")
end
end algorithm
Sortie Hello, world!hello.aml sur GitHubEssayerBientôt

minutes.aml

Convertir des minutes en heures et minutes.Expliqué dans Votre premier programme
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
Entrée 135Sortie 135 minutes = 2 h 15 minminutes.aml sur GitHubEssayerBientôt

swap.aml

Échanger deux variables avec une troisième.
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
Sortie Before: a = 4, b = 9 After: a = 9, b = 4swap.aml sur GitHubEssayerBientôt

temperature.aml

Convertir des degrés Celsius en 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
Entrée 37.5Sortie 37.5 C = 99.5 Ftemperature.aml sur GitHubEssayerBientôt

Conditions

calculator.aml

Les quatre opérations choisies dans un 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
Entrée 7 2 4Sortie 7 / 2 = 3.5calculator.aml sur GitHubEssayerBientôt

even_or_odd.aml

Un nombre est-il pair ou impair ?
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
Entrée 27Sortie 27 is oddeven_or_odd.aml sur GitHubEssayerBientôt

grade.aml

Transformer une note en mention avec elseif.Expliqué dans 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
Entrée 13Sortie Goodgrade.aml sur GitHubEssayerBientôt

leap_year.aml

Une année est-elle bissextile ?
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
Entrée 2024Sortie 2024 is a leap yearleap_year.aml sur GitHubEssayerBientôt

max_of_three.aml

Le plus grand de trois nombres.Expliqué dans 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
Entrée 7 19 4Sortie The largest is 19max_of_three.aml sur GitHubEssayerBientôt

operators.aml

div, mod, AND, NOT et affectation composée.Expliqué dans Expressions et opérateurs
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
Sortie 3 2 a is odd and greater than b 20operators.aml sur GitHubEssayerBientôt

sign.aml

Positif, négatif ou nul.
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
Entrée -8Sortie negativesign.aml sur GitHubEssayerBientôt

triangle.aml

Type d'un triangle à partir de ses trois côtés.
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
Entrée 5 5 8Sortie Isoscelestriangle.aml sur GitHubEssayerBientôt

weekday.aml

Nom du jour avec switch.Expliqué dans 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
Entrée 7Sortie Sunday: weekendweekday.aml sur GitHubEssayerBientôt

Boucles

collatz.aml

Étapes de la suite de Syracuse jusqu'à 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
Entrée 27Sortie Reached 1 in 111 stepscollatz.aml sur GitHubEssayerBientôt

countdown.aml

Compte à rebours avec 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
Sortie 5 4 3 2 1 Go!countdown.aml sur GitHubEssayerBientôt

digit_sum.aml

Somme des chiffres d'un nombre.
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
Entrée 90517Sortie Sum of the digits: 22digit_sum.aml sur GitHubEssayerBientôt

even_numbers.aml

Les nombres pairs de 2 à 20.Expliqué dans Boucles
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
Sortie 2 4 6 8 10 12 14 16 18 20even_numbers.aml sur GitHubEssayerBientôt

loops.aml

Boucles for, avec et sans pas.Expliqué dans Boucles
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
Sortie 1 2 3 1 3 5 7 9loops.aml sur GitHubEssayerBientôt

password.aml

Demander un mot de passe jusqu'au bon.
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
Entrée hello AlgoML algomlSortie Password: Password: Password: Welcome! 3 triespassword.aml sur GitHubEssayerBientôt

positive.aml

Redemander tant que la valeur n'est pas valide.Expliqué dans Boucles
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
Entrée -4 0 9Sortie Enter a positive number: Enter a positive number: Enter a positive number: Thank you, you entered 9positive.aml sur GitHubEssayerBientôt

power.aml

Puissance d'un nombre avec une boucle.
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
Entrée 3 5Sortie 3^5 = 243power.aml sur GitHubEssayerBientôt

reverse_number.aml

Écrire un nombre à l'envers.
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
Entrée 12345Sortie 54321reverse_number.aml sur GitHubEssayerBientôt

stars.aml

Un triangle d'étoiles avec des boucles imbriquées.
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
Sortie * ** *** **** *****stars.aml sur GitHubEssayerBientôt

sum_to_n.aml

Somme des nombres de 1 à 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
Entrée 100Sortie 1 + 2 + ... + 100 = 5050sum_to_n.aml sur GitHubEssayerBientôt

Fonctions et procédures

absolute.aml

Valeur absolue d'un 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

Factorielle avec une fonction récursive.Expliqué dans 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
Entrée 5Sortie 5! = 120factorial.aml sur GitHubEssayerBientôt

fibonacci.aml

Les dix premiers nombres de Fibonacci.
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
Sortie 1 1 2 3 5 8 13 21 34 55fibonacci.aml sur GitHubEssayerBientôt

gcd.aml

Plus grand commun diviseur, méthode d'Euclide.Expliqué dans Structure d'un programme
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
Entrée 48 36Sortie gcd = 12gcd.aml sur GitHubEssayerBientôt

hanoi.aml

Tours de Hanoï avec une procédure récursive.
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
Sortie 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 sur GitHubEssayerBientôt

is_prime.aml

Nombres premiers jusqu'à 20 avec une fonction BOOLEAN.Expliqué dans Fonctions et procédures
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
Sortie 2 3 5 7 11 13 17 19is_prime.aml sur GitHubEssayerBientôt

lcm.aml

Plus petit commun multiple, à partir du pgcd.
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
Sortie lcm(12, 18) = 36lcm.aml sur GitHubEssayerBientôt

min_max.aml

Deux petites fonctions : minimum et 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
Sortie min = 9, max = 14min_max.aml sur GitHubEssayerBientôt

power_recursive.aml

Puissance d'un nombre, version récursive.
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

Table de multiplication dans une procédure.Expliqué dans Fonctions et procédures
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
Sortie 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35table.aml sur GitHubEssayerBientôt

Tableaux

count_occurrences.aml

Nombre d'apparitions d'une valeur.
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
Entrée 3Sortie 3 appears 4 timescount_occurrences.aml sur GitHubEssayerBientôt

max_in_array.aml

Plus grande valeur d'un tableau.Expliqué dans Tableaux
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

Remplir un tableau au clavier.
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
Entrée 4 8 15 16 23Sortie Total: 66read_array.aml sur GitHubEssayerBientôt

reverse_array.aml

Inverser un tableau sur 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

Plus grande et deuxième plus grande valeur.
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
Sortie Largest: 45, second: 30second_largest.aml sur GitHubEssayerBientôt

shift_left.aml

Décaler un tableau d'une case vers la gauche.
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
Sortie 20 30 40 50 10shift_left.aml sur GitHubEssayerBientôt

sum_and_mean.aml

Somme et moyenne des valeurs d'un tableau.Expliqué dans Tableaux
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
Sortie Sum: 65 Mean: 13sum_and_mean.aml sur GitHubEssayerBientôt

Structures

bank_account.aml

Un compte bancaire avec un solde REAL.
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
Sortie Ada has 130.5 on her accountbank_account.aml sur GitHubEssayerBientôt

date.aml

Laquelle de deux dates vient en premier.
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
Sortie The first date comes firstdate.aml sur GitHubEssayerBientôt

point.aml

Distance au carré entre deux 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
Sortie Distance squared: 25point.aml sur GitHubEssayerBientôt

rectangle.aml

Une structure passée à une fonction.Expliqué dans Structures et 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

Une structure avec un nom et une note.Expliqué dans Structures et 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
Sortie Ada got 15/20student.aml sur GitHubEssayerBientôt

Tris et recherches

bubble_sort.aml

Tri à bulles.Expliqué dans Tableaux
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

Tri par insertion.
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
Sortie 26 31 41 41 58 59insertion_sort.aml sur GitHubEssayerBientôt

selection_sort.aml

Tri par sélection.
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
Sortie 5 10 13 14 21 29 37selection_sort.aml sur GitHubEssayerBientôt

Problèmes sur les nombres

armstrong.aml

Nombres d'Armstrong à trois chiffres.
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
Sortie 153 370 371 407armstrong.aml sur GitHubEssayerBientôt

divisors.aml

Tous les diviseurs d'un nombre.
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
Entrée 36Sortie 1 2 3 4 6 9 12 18 36divisors.aml sur GitHubEssayerBientôt

pascal.aml

Triangle de Pascal, un tableau mis à jour ligne par ligne.
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
Sortie 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 sur GitHubEssayerBientôt

perfect_numbers.aml

Nombres parfaits inférieurs à 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

Décomposition en facteurs premiers.
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
Entrée 360Sortie 2 2 2 3 3 5prime_factors.aml sur GitHubEssayerBientôt

sieve.aml

Crible d'Ératosthène jusqu'à 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
Sortie 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47sieve.aml sur GitHubEssayerBientôt