TP07 Boucles et Boucles (suite)
This commit is contained in:
parent
33bfc43144
commit
8a59a181a1
20
APL1.1/TP07/bornes.c
Normal file
20
APL1.1/TP07/bornes.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
int arg, min, max;
|
||||
printf("Entrez des valeurs (-1 pour arreter) : ");
|
||||
scanf("%d", &arg);
|
||||
min = arg;
|
||||
max = arg;
|
||||
|
||||
while(arg != -1) {
|
||||
min = arg < min ? arg : min;
|
||||
max = arg > max ? arg : max;
|
||||
scanf("%d", &arg);
|
||||
}
|
||||
|
||||
printf("Minimum : %d\nMaximum : %d\n", min, max);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
26
APL1.1/TP07/devinettes.c
Normal file
26
APL1.1/TP07/devinettes.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<time.h>
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
srand(time(NULL));
|
||||
int ans = rand() % 100;
|
||||
int guess;
|
||||
|
||||
for (int attempt = 0; attempt < 5; attempt++) {
|
||||
printf("%d essai(s) restant(s)\n", 5 - attempt);
|
||||
scanf("%d", &guess);
|
||||
|
||||
if (guess == ans) {
|
||||
printf("Bravo, vous avez trouvé !\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if (guess > ans) printf("La réponse est - grande.\n");
|
||||
else printf("La réponse est + grande.\n");
|
||||
}
|
||||
|
||||
printf("Dommage, la bonne réponse était %d.\n", ans);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
26
APL1.1/TP07/division.c
Normal file
26
APL1.1/TP07/division.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int arg1, arg2, arg3;
|
||||
|
||||
printf("Veuillez entrer un entier positif : ");
|
||||
scanf("%d", &arg1);
|
||||
arg3 = arg1;
|
||||
|
||||
printf("Veuillez entrer un entier supérieur à 0 : ");
|
||||
scanf("%d", &arg2);
|
||||
|
||||
int quotient, reste;
|
||||
quotient = 0;
|
||||
|
||||
while ( arg1 >= arg2 ) {
|
||||
quotient++;
|
||||
arg1 -= arg2;
|
||||
}
|
||||
|
||||
reste = arg1;
|
||||
printf("La division euclidienne de %d par %d est : \n", arg3, arg2);
|
||||
printf("Quotient : %d\n", quotient);
|
||||
printf("Reste : %d\n", reste);
|
||||
}
|
40
APL1.1/TP07/facteurs.c
Normal file
40
APL1.1/TP07/facteurs.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int prime(int value) {
|
||||
for (int diviseur = 2; diviseur < value; diviseur++) {
|
||||
if (value == 1 ) {
|
||||
return 0;
|
||||
} else if (value % diviseur == 0 && diviseur != value) {
|
||||
return 0;
|
||||
} else if (value == 2) return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int value, remainder;
|
||||
|
||||
printf("Veuillez donner un entier naturel : ");
|
||||
scanf("%d", &value);
|
||||
remainder = value;
|
||||
|
||||
printf("%d = ", value);
|
||||
|
||||
while (!prime(remainder)) {
|
||||
int factor;
|
||||
|
||||
for (int i = 2; i < remainder; i++) {
|
||||
if (prime(i) && remainder % i == 0) {
|
||||
factor = i;
|
||||
remainder = remainder / factor;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d*", factor);
|
||||
}
|
||||
printf("%d\n", remainder);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
63
APL1.1/TP07/figures.c
Normal file
63
APL1.1/TP07/figures.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<math.h>
|
||||
|
||||
int sq(int x) {
|
||||
return x*x;
|
||||
}
|
||||
|
||||
int getmaxsq(int value) {
|
||||
int max_square = 0;
|
||||
|
||||
for (int i = 0; i <= value; i++) {
|
||||
if ( value - sq(i) >= 0) max_square = i;
|
||||
else break;
|
||||
}
|
||||
|
||||
return max_square;
|
||||
}
|
||||
|
||||
void printr(int value, int v1, int v2, int v3, int v4) {
|
||||
printf("%d = %d + %d + %d + %d\n", value, sq(v1), sq(v2), sq(v3), sq(v4));
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int value;
|
||||
|
||||
printf("Veuillez donner un entier naturel : ");
|
||||
scanf("%d", &value);
|
||||
|
||||
int ms1 = getmaxsq(value);
|
||||
for (int i1 = ms1; i1 > 0 && sq(i1)*4 >= value && value - sq(i1) < sq(i1+1); i1--) {
|
||||
int val1 = value - sq(i1);
|
||||
|
||||
if (val1 == 0) printr(value, i1, 0, 0, 0);
|
||||
else {
|
||||
int ms2 = getmaxsq(val1);
|
||||
for (int i2 = ms2; i2 > 0 && sq(i2)*4 >= val1 && val1 - sq(i2) < sq(i2+1); i2--) {
|
||||
int val2 = val1 - sq(i2);
|
||||
|
||||
if (val2 == 0) printr(value, i1, i2, 0, 0);
|
||||
else {
|
||||
int ms3 = getmaxsq(val2);
|
||||
for (int i3 = ms3; i3 > 0 && sq(i3)*4 >= val2 && val2 - sq(i3) < sq(i3+1); i3--) {
|
||||
int val3 = val2 - sq(i3);
|
||||
|
||||
if (val3 == 0) printr(value, i1, i2, i3, 0);
|
||||
else {
|
||||
int ms4 = getmaxsq(val3);
|
||||
for (int i4 = ms4; i4 > 0 && sq(i4)*4 >= val3 && val3 - sq(i4) < sq(i4+1); i4--) {
|
||||
int val4 = val3 - sq(i4);
|
||||
|
||||
if (val4 == 0) printr(value, i1, i2, i3, i4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
18
APL1.1/TP07/filtrage_bac.c
Normal file
18
APL1.1/TP07/filtrage_bac.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int note = 0;
|
||||
|
||||
do {
|
||||
printf("Veuillez donner votre note du bac : ");
|
||||
scanf("%d", ¬e);
|
||||
} while ( note < 10 || note > 20);
|
||||
|
||||
if (note >= 16) printf("Vous avez eu mention 'Très Bien'.\n");
|
||||
else if (note >= 14) printf("Vous avez eu mention 'Bien'.\n");
|
||||
else if (note >= 12) printf("Vous avez eu mention 'Assez Bien'.\n");
|
||||
else printf("Vous n'avez pas eu de mention.\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
22
APL1.1/TP07/premier.c
Normal file
22
APL1.1/TP07/premier.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int entier;
|
||||
|
||||
printf("Veuillez donner un entier naturel : ");
|
||||
scanf("%d", &entier);
|
||||
|
||||
for (int diviseur = 2; diviseur < entier; diviseur++) {
|
||||
if (entier == 1 ) {
|
||||
printf("Cet entier n'est pas premier.\n");
|
||||
return EXIT_SUCCESS;
|
||||
} else if (entier % diviseur == 0 && diviseur != entier) {
|
||||
printf("Cet entier n'est pas premier.\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Cet entier est premier.\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
27
APL1.1/TP07/progression.c
Normal file
27
APL1.1/TP07/progression.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int desired_index, value;
|
||||
|
||||
printf("Saisissez le numéro de terme souhaité : ");
|
||||
scanf("%d", &desired_index);
|
||||
|
||||
int index_m2 = 0;
|
||||
int index_m1 = 1;
|
||||
|
||||
if (desired_index < 2 && desired_index >= 0 ) value = desired_index;
|
||||
else if (desired_index > 1) {
|
||||
for (int index = 2; index <= desired_index; index++) {
|
||||
value = index_m2 + index_m1;
|
||||
index_m2 = index_m1;
|
||||
index_m1 = value;
|
||||
}
|
||||
} else {
|
||||
printf("Valeur invalide.\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
printf("Le %dème terme de la suite est : %d\n", desired_index, value);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
16
APL1.1/TP07/sequence.c
Normal file
16
APL1.1/TP07/sequence.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int arg1, arg2;
|
||||
printf("Veuillez donner deux entiers séparés d'un espace : ");
|
||||
scanf("%d %d", &arg1, &arg2);
|
||||
|
||||
printf("Les nombres compris entre %d et %d sont :\n", arg1, arg2);
|
||||
for (; arg1 <= arg2; arg1++) {
|
||||
printf("%d ", arg1);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
15
APL1.1/TP07/table.c
Normal file
15
APL1.1/TP07/table.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int table;
|
||||
|
||||
printf("Veuillez donner un entier : ");
|
||||
scanf("%d", &table);
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
printf("%d * %2d = %3d\n", table, i, table * i);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
33
APL1.1/TP07/table_3.c
Normal file
33
APL1.1/TP07/table_3.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
#define TABLE_WIDTH 15
|
||||
#define TABLE_HEIGHT 15
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
|
||||
//En-tête de la table sur toute la ligne.
|
||||
printf(" X | ");
|
||||
for (int col = 0; col <= TABLE_WIDTH; col++) {
|
||||
printf(" %3d ", col);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
//Séparation de la table sur toute la ligne.
|
||||
printf("-----+");
|
||||
for (int col = 0; col <= TABLE_WIDTH; col++) {
|
||||
printf("-----", col);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
//Affichage de la table de multiplication + en-tête
|
||||
for (int lin = 0; lin <= TABLE_HEIGHT; lin++) {
|
||||
printf(" %3d | ", lin);
|
||||
for (int col = 0; col <= TABLE_WIDTH; col++) {
|
||||
printf(" %3d ", lin * col);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
17
APL1.1/TP07/tables.c
Normal file
17
APL1.1/TP07/tables.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int table;
|
||||
|
||||
while(1) {
|
||||
printf("Veuillez donner un entier : ");
|
||||
scanf("%d", &table);
|
||||
|
||||
if (table == -1) return EXIT_SUCCESS;
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
printf("%d * %d = %d\n", i, table, i*table);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user