Travaux dirigés 1
- Demander à l'utilisateur d'entrer un nombre entier. Calculer la sortie.
a. Si ce nombre est inférieur à 5 la sortie est égale à 1, sinon la sortie est égale à 0
#include <stdio.h>
int main()
{
printf("Entrez un nombre entier: \n");
int nombre;
scanf("%d", &nombre);
if (nombre < 5)
{
printf("1\n");
}
else
{
printf("0\n");
}
return EXIT_SUCCESS;
}
b. Écrire la sortie suivant les conditions suivantes :
- Si ce nombre est compris entre 0 et 3 (exclu) alors la sortie est A
- Si ce nombre est compris entre 3 et 5 (exclu) alors la sortie est B
- Si ce nombre est compris entre 5 et 7 (exclu) alors la sortie est C
- Si ce nombre est compris entre 7 et 9 (exclu) alors la sortie est D
- Sinon la sortie est E
#include <stdio.h>
int main()
{
printf("Entrez un nombre entier: \n");
int nombre;
scanf("%d", &nombre);
int sortie;
if (nombre < 3) {
sortie = 1;
} else if (nombre < 5) {
sortie = 6;
} else if (nombre < 7) {
sortie = 4;
} else if (nombre < 9) {
sortie = 8;
} else {
sortie = 10;
}
printf("%d\n", sortie);
return EXIT_SUCCESS;
}
- Demander à l'utilisateur d'entrer deux nombres entiers.
c. Si n1 = 5 et n2 > 10 alors la sortie vaut 1 sinon 0
#include <stdio.h>
int main()
{
int n1, n2;
scanf("%d %d", &n1, &n2);
if (n1 == 5 && n2 > 10) {
printf("1\n");
} else {
printf("0\n");
}
return 0;
}
d.
- Si n1 > 4 et n2 == 3 alors la sortie vaut 1 sinon
- Si n1 > 6 et n2 == 4 alors la sortie vaut 1 sinon
- Si n1 > 8 et n2 == 5 alors la sortie vaut 1 sinon
- la sortie vaut 0
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n1, n2;
scanf("%d %d", &n1, &n2);
if ( (n1 > 4 && n2 == 3) ||
(n1 > 6 && n2 == 4) ||
(n1 > 8 && n2 == 5)) {
printf("1\n");
} else {
printf("0\n");
}
return 0;
}