10 Exercices corrigés – Algorithmique
Sciences d’Ingénieur – 2ème BAC STE
Exercice 1 : Maximum de 3 nombres
Écrire un algorithme qui trouve le maximum de 3 nombres a, b, c.
Correction :
ALGORITHME Maximum
DÉBUT
SI a > b ET a > c ALORS
max ← a
SINON SI b > c ALORS
max ← b
SINON
max ← c
FINSI
AFFICHER max
FIN
Exercice 2 : Somme des entiers pairs
Écrire un algorithme qui calcule la somme des entiers pairs inférieurs ou égaux à N.
Correction :
ALGORITHME SommePairs
DÉBUT
somme ← 0
POUR i DE 0 À N PAS 2 FAIRE
somme ← somme + i
FINPOUR
AFFICHER somme
FIN
Exercice 3 : Factorielle
Écrire un algorithme qui calcule la factorielle d’un nombre N.
Correction :
ALGORITHME Factorielle
DÉBUT
fact ← 1
POUR i DE 1 À N FAIRE
fact ← fact * i
FINPOUR
AFFICHER fact
FIN
Exercice 4 : Nombre premier
Écrire un algorithme qui vérifie si un nombre N est premier.
Correction :
ALGORITHME Premier
DÉBUT
premier ← VRAI
SI N ≤ 1 ALORS
premier ← FAUX
SINON
POUR i DE 2 À √N FAIRE
SI N % i = 0 ALORS
premier ← FAUX
FINSI
FINPOUR
FINSI
AFFICHER premier
FIN
Exercice 5 : Inversion de tableau
Écrire un algorithme qui inverse les éléments d’un tableau T de taille N.
Correction :
ALGORITHME InverserTableau
DÉBUT
POUR i DE 0 À (N/2)-1 FAIRE
temp ← T[i]
T[i] ← T[N-i-1]
T[N-i-1] ← temp
FINPOUR
FIN
Exercice 6 : Recherche dichotomique
Écrire un algorithme qui recherche une valeur dans un tableau trié en utilisant la méthode dichotomique.
Correction :
ALGORITHME Dichotomie
DÉBUT
debut ← 0
fin ← N-1
trouve ← FAUX
TANTQUE debut ≤ fin ET NON trouve FAIRE
milieu ← (debut + fin) DIV 2
SI T[milieu] = valeur ALORS
trouve ← VRAI
SINON SI T[milieu] < valeur ALORS
debut ← milieu + 1
SINON
fin ← milieu – 1
FINSI
FINTANTQUE
AFFICHER trouve
FIN
Exercice 7 : PGCD (Algorithme d’Euclide)
Écrire un algorithme qui calcule le PGCD de deux nombres a et b.
Correction :
ALGORITHME PGCD
DÉBUT
TANTQUE b ≠ 0 FAIRE
temp ← b
b ← a MOD b
a ← temp
FINTANTQUE
AFFICHER a
FIN
Exercice 8 : Fibonacci
Écrire un algorithme qui affiche les N premiers termes de la suite de Fibonacci.
Correction :
ALGORITHME Fibonacci
DÉBUT
SI N ≥ 1 ALORS AFFICHER 0
SI N ≥ 2 ALORS AFFICHER 1
a ← 0
b ← 1
POUR i DE 3 À N FAIRE
c ← a + b
AFFICHER c
a ← b
b ← c
FINPOUR
FIN
Exercice 9 : Tri par sélection
Écrire un algorithme qui trie un tableau T de taille N par la méthode de sélection.
Correction :
ALGORITHME TriSelection
DÉBUT
POUR i DE 0 À N-2 FAIRE
min ← i
POUR j DE i+1 À N-1 FAIRE
SI T[j] < T[min] ALORS
min ← j
FINSI
FINPOUR
SI min ≠ i ALORS
temp ← T[i]
T[i] ← T[min]
T[min] ← temp
FINSI
FINPOUR
FIN
Exercice 10 : Conversion binaire
Écrire un algorithme qui convertit un nombre décimal en binaire.
Correction :
ALGORITHME DecimalVersBinaire
DÉBUT
SI n = 0 ALORS
AFFICHER “0”
SINON
binaire ← “”
TANTQUE n > 0 FAIRE
reste ← n MOD 2
binaire ← reste & binaire
n ← n DIV 2
FINTANTQUE
AFFICHER binaire
FINSI
FIN