235 lines
3.7 KiB
Plaintext
235 lines
3.7 KiB
Plaintext
|
-------- TP07 : Boucles --------
|
||
|
|
||
|
1.
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
|
||
|
/* Version 1 : while */
|
||
|
|
||
|
int main(void) {
|
||
|
int a;
|
||
|
int b;
|
||
|
printf("Entrez un entier : ");
|
||
|
scanf("%d", &a);
|
||
|
getchar();
|
||
|
printf("Entrez un autre entier : ");
|
||
|
scanf("%d", &b);
|
||
|
getchar();
|
||
|
if (a < b) {
|
||
|
while (a <= b) {
|
||
|
printf("%d\n", a);
|
||
|
a++;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
while (b <= a) {
|
||
|
printf("%d\n", b);
|
||
|
b++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
--------------------------------
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
|
||
|
/* Version 2 : do...while */
|
||
|
|
||
|
int main(void) {
|
||
|
int a;
|
||
|
int b;
|
||
|
printf("Entrez un entier : ");
|
||
|
scanf("%d", &a);
|
||
|
getchar();
|
||
|
printf("Entrez un autre entier : ");
|
||
|
scanf("%d", &b);
|
||
|
getchar();
|
||
|
if (a < b) {
|
||
|
do {
|
||
|
printf("%d\n", a);
|
||
|
a++;
|
||
|
} while (a <= b);
|
||
|
}
|
||
|
else {
|
||
|
do {
|
||
|
printf("%d\n", b);
|
||
|
b++;
|
||
|
} while (b <= a);
|
||
|
}
|
||
|
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
---------------------
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
|
||
|
/* Version 3 : for */
|
||
|
|
||
|
int main(void) {
|
||
|
int a;
|
||
|
int b;
|
||
|
printf("Entrez un entier : ");
|
||
|
scanf("%d", &a);
|
||
|
getchar();
|
||
|
printf("Entrez un autre entier : ");
|
||
|
scanf("%d", &b);
|
||
|
getchar();
|
||
|
if (a < b) {
|
||
|
for (;a <= b; a++) {
|
||
|
printf("%d\n", a);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
for (;b <= a; b++) {
|
||
|
printf("%d\n", b);
|
||
|
b++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
---------------------------------
|
||
|
|
||
|
2.
|
||
|
|
||
|
# include <stdlib.h>
|
||
|
# include <stdio.h>
|
||
|
|
||
|
int main(void) {
|
||
|
double montant;
|
||
|
printf("Combien d'euros souhaitez vous investir ?\n -->");
|
||
|
scanf("%lf", &montant);
|
||
|
for (; montant < 1000 || montant > 50000; scanf("%lf", &montant)) {
|
||
|
printf("Veuillez entrer une valeur valide (entre 1000 et 50000).\n -->");
|
||
|
}
|
||
|
/* Le code ci-après pourrait être amélioré avec une boucle mais ce n'est pas demandé */
|
||
|
printf("%9.4f\n", montant);
|
||
|
montant = montant + (4*montant/100);
|
||
|
printf("%9.4f\n", montant);
|
||
|
montant = montant + (4*montant/100);
|
||
|
printf("%9.4f\n", montant);
|
||
|
montant = montant + (4*montant/100);
|
||
|
printf("%9.4f\n", montant);
|
||
|
montant = montant + (4*montant/100);
|
||
|
printf("%9.4f\n", montant);
|
||
|
montant = montant + (4*montant/100);
|
||
|
printf("%9.4f\n", montant);
|
||
|
montant = montant + (4*montant/100);
|
||
|
printf("%9.4f\n", montant);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
--------------------------
|
||
|
|
||
|
3.
|
||
|
|
||
|
# include <stdlib.h>
|
||
|
# include <stdio.h>
|
||
|
|
||
|
int main(void) {
|
||
|
int n;
|
||
|
int i;
|
||
|
printf("Entrez un entier : ");
|
||
|
scanf("%d", &n);
|
||
|
for (i = 1; i <= 10; i++) {
|
||
|
printf(" %d x %d = %d\n",n,i,n*i);
|
||
|
}
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
---------------------------
|
||
|
|
||
|
4.
|
||
|
|
||
|
# include <stdlib.h>
|
||
|
# include <stdio.h>
|
||
|
|
||
|
int main(void) {
|
||
|
int n;
|
||
|
int x;
|
||
|
int compteur;
|
||
|
int nbis;
|
||
|
compteur = 0;
|
||
|
printf("Entrez un entier >= 0 : ");
|
||
|
scanf("%d", &n);
|
||
|
printf("Entrez un entier > 0 : ");
|
||
|
scanf("%d", &x);
|
||
|
nbis = n;
|
||
|
for (; nbis >= x; nbis -= x) {
|
||
|
compteur++;
|
||
|
}
|
||
|
printf("\n--> %d = %d x %d + %d\n",n,compteur,x,nbis);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
-----------------------
|
||
|
|
||
|
5.
|
||
|
|
||
|
|
||
|
# include <stdlib.h>
|
||
|
# include <stdio.h>
|
||
|
|
||
|
int main(void) {
|
||
|
int table;
|
||
|
int i;
|
||
|
printf("---- Affichage de tables ----\n");
|
||
|
printf("Quelle table voulez-vous afficher ? (-1 pour arrêter) ");
|
||
|
scanf("%d", &table);
|
||
|
while (table != -1) {
|
||
|
for (i = 1; i <= 10; i++) {
|
||
|
printf(" %d x %d = %d\n",table,i,table*i);
|
||
|
}
|
||
|
printf("Quelle table voulez-vous afficher ? (-1 pour arrêter) ");
|
||
|
scanf("%d", &table);
|
||
|
}
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
---------------------
|
||
|
|
||
|
6.
|
||
|
|
||
|
# include <stdlib.h>
|
||
|
# include <stdio.h>
|
||
|
|
||
|
int main(void) {
|
||
|
int saisie;
|
||
|
int min;
|
||
|
int max;
|
||
|
printf("Entrez un entier (-1 pour arrêter) ");
|
||
|
scanf("%d", &saisie);
|
||
|
min = saisie;
|
||
|
max = saisie;
|
||
|
do {
|
||
|
printf("Entrez un entier (-1 pour arrêter) ");
|
||
|
scanf("%d", &saisie);
|
||
|
if (saisie < min && saisie != -1) {
|
||
|
min = saisie;
|
||
|
}
|
||
|
if (saisie > max) {
|
||
|
max = saisie;
|
||
|
}
|
||
|
} while (saisie != -1);
|
||
|
printf("Minimum : %d Maximum : %d\n",min,max);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|