Nom : ...........................................................
Réservé à l’administration :
Prénom : .....................................................
Date de naissance : ........./........./................ CODE : .....................
Auteur : Mohamed Messabihi Université Abou Bakr Belkaïd - Tlemcen
Matière : Initiation à l’algorithmique Faculté des Sciences
Date 22 mars 2021 1ère Année MI
Durée 1h30 Semestre 1
Correction de l’examen final
Aucun document n'est autorisé
Les solutions doivent être rédigées en C
Note CC = Exo1 x 2 (Achage)
Note EF = Exo1 + Exo2 + Exo3
1 Affichage 10 pts. U40’
Pour chaque programme ci-dessous, cochez la bonne réponse qui correspond à son affichage
ZRemarque : il y a exactement une seule bonne réponse. 1 pt pour une bonne réponse et 0 pour une
mauvaise réponse ou des réponses multiples pour la même question.
2 i=1, j=2
1 int main () {
2 int i =1 , j =2;
2 i=2, j=4
3 i = j *2 ; 2 i=4, j=4
4 j = i *2;
5 printf ( " i = %d , j = % d " ,i , j ) ; i=4, j=8
6 } 2 i=4, j=2
2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 int main () { 2 x=1, y=2, z=3
int x =1 , y =2 , z =3 ;
2
2
3 x = y ; x=2, y=3, z=1
4 y = z; x=3, y=3, z=3
5 x= z ;
6 printf ( " x = %d , y = %d , z = % d " ,x ,y , z ) ; 2 x=3, y=1, z=2
7 }
2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 void main () {
2 int a = 10 , b = 3;
2 a=0, b=1
3 a = a %b ; a=1, b=0
4 b = b %a ;
5 printf ( " a =% d , b =% d " , a , b ) ; 2 a=1, b=3
6 } 2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 void main () {
2 int a =1 , b =0; a=0, b=1
3
4
if ( a )
b ++;
2 a=1, b=0
5 if ( b ) 2 a=0, b=0
6
7
a - -;
printf ( " a =% d , b =% d" , a , b ) ;
2 a=1, b=1
8 } 2 Autre réponse : . . . . . . . . . . . . . . . . . .
1/4
1 void main () {
2 int a =1 , b =0 , c = 1;
2 faux vrai
3 if (!( a < c ) ||( a - b ) &&( a + c ) ||( a && b ) ) 2 vrai faux
4
5 else
printf ( " faux " ) ;
2 vrai
6 printf ( " vrai " ) ; faux
7 }
2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 void main () {
2 int i = 0; 2 012345678
3 for (i =1; i <9; i ++) 1357
4 {
5 printf ( " % d " , i ) ; 2 12345678
6
7 }
i = i +1;
2 01357
8 } 2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 void main () {
2 int c =3; 2 210
3 while ( c )
4 switch ( c )
2 2110
5 { 2 3210
6 case 0 : printf ( "% d " , c ) ; break ;
7 case 1 : printf ( "% d " , c +1) ; 2120
8
9 }
default : c - -; printf ( " % d " , c ) ; break ; 2 2121
10 } 2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 void main ()
2 { 2 6 8 10 12 14 16
3 int i =0 , j = 6;
4 while (i <j )
6 9 12 15 18 21
5 { 2 6 7 8 9 10 11
printf ( " % d " , i + j ) ;
6
7 i = i +2; 2 6 9 12 15 18
8
9 }
j ++; 2 6 9 12 15 18 21 24
10 } 2 Autre réponse : . . . . . . . . . . . . . . . . . .
1 void main () {
2 int a =0 , b = 10; 2 0123456789
while (a <b )
2
3
4 { 01234
5
6
a ++;
b ++;
2 0 2 4 6 8 10
7 } 2 0 2 4 6 8 10 12 16 18
8 printf ( " % d " , a + b ) ;
9 }
Autre réponse : boucle infinie
1 void main () {
int i =0 , j =5 , s =0;
2
3 while (i <j ) 2 5 9 12 14
4
5
while ( j )
{
2 5 9 12 14 15
6 j - -; 4 7 9 10 10
7
8
s = s + i+ j ;
printf ( " % d " ,s ) ;
2 4 7 9 10
9 } 2 Autre réponse : . . . . . . . . . . . . . . . . . .
10 }
2/4
2 Produit de deux nombres 4 pts. U20’
Écrire un programme qui demande à l’utilisateur deux entiers (n et m) et affiche ensuite si leur produit
(n*m) est strictement négatif, strictement positif ou nul.
ZAttention : le programme ne doit pas calculer le produit des deux nombres.
Solution
1 # include < stdio .h >
2 void main ()
3 {
4 int a , b , val ;
5 printf ( " Donner une valeur pour n ") ;
6 scanf ( " % d " , & a ) ;
7 printf ( " Saisir une valeur pour m ") ;
8 scanf ( " % d " , & b ) ;
9 if ( n ==0 || m ==0)
10 {
11 printf ( " Le produit est nul " ) ;
12 }
13 else if (( a > 0 && b > 0) || ( a < 0 && b < 0) )
14 {
15 printf ( " Le produit est positif " ) ;
16 }
17 else
18 {
19 printf ( " Le produit est négatif " ) ;
20 }
21 }
3 La somme d’une série 6 pts. U30’
Écrire un programme qui demande à l’utilisateur un entier n, et calcule ensuite la somme des n termes
de la série suivante : 1 + 11 + 111 + 1111 + · · · + 11111...1 (le nième terme contient n fois le chiffre 1).
Par exemple : si l’utilisateur saisie la valeur 5 (pour n), alors l’exécution de votre programme devrait avoir
l’affichage ci-dessous :
Donnez le nombre de termes : 5
1 + 11 + 111 + 1111 + 11111
La somme est : 12345
ZAttention : L’affichage de la série ainsi que la somme comme indiqué dans l’exemple est important.
Solution
3/4
1 # include < stdio .h >
2 void main ()
3 {
4 int n , i ;
5 int som =0;
6 int t =1;
7 printf ( " Donnez le nombre de termes : " ) ;
8 scanf ( " % d " ,& n ) ;
9 for ( i =1; i <= n ; i ++)
10 {
11 printf ( " % d " ,t ) ;
12 if (i < n )
13 {
14 printf ( " + " ) ;
15 }
16 som = som + t ;
17 t =( t *10) +1;
18 }
19 printf ( " \ n La somme est : % ld \ n " , som ) ;
20 }
Bon courage »
4/4