Vous êtes sur la page 1sur 43

Algorithmique et programmation

Chapitre 5:Introduction à
la programmation C
1. Introduction
2. Variable, saisie, calcul, affichage
3. Structures alternatives
4. Structures itératives

1
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Introduction
• Un programme en c : un programme écrit en
langage c qui traduit un algorithme décrivant la
solution d’un problème.
• Il est composé de:
• Directives de préprocesseur (#)
• Déclarations
• Fonctions
• Fonction principale (main)
•Instruction
• Bloc
• Commentaires (/* texte*/ , // ligne)
2
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Introduction (suite)
Exemple:
Bibliothèque
# include<stdio.h>
Directive float Rayon,Pi=3.14; Déclaration de données
float Surface(float r)
{ return (r*r*Pi);} Fonction
main()
{ Fonction principale
float Res;
Instruction
Scanf("%f",&Rayon);
Bloc Res = Surface(Rayon); /* Appel de la fonction*/
printf ("%f",Res);
} Commentaire

3
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Donnée
• Donnée: constituée de trois attributs
(identificateur, valeur et type)
• Mots réservés au langage c:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

4
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Donnée (suite)
• Type: Nom Taille
char 1 octet

int 2 octets (et plus)


short int 2 octets
long int 4 octets
unsigned int 2 octets
float 4 octets
double 8 octets
long Double 10 octets

• Catégories de donnée: variable et constante

5
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Donnée (suite)
• Variable:
type identificateur_var ;
Exemple: float surface ;
• Constante:
const type identificateur_const = valeur ;
Exemple: const float Pi = 3.14;
• Remarques:
•On peut utiliser # et define pour définir une
constante (#define Pi 3.14)
• Deux types de variable: globale et locale
6
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Calcul

• Expression: combinaison d’opérandes et


d’opérateurs.
•Affectation: stockage du résultat d'une
expression dans une variable.
identificateur_variable = expression ;
Exemple: surface = rayon*rayon*3.14;

7
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Calcul (suite)

• Opérateurs:
• Arithmétiques
• Relationnels
• Logiques
• Incrémentation / décrémentation
•…

8
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Calcul (suite)

• Opérateurs arithmétiques:
Opérateur Signification
+ Addition
- Soustraction
* Multiplication
/ Division
% Reste de la
division entière

9
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Calcul (suite)

• Opérateurs relationnels:
Opérateur Signification
== Egal
!= Différent
< Inférieur
> Supérieur
<= Inférieur ou égal
>= Supérieur ou égal

10
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Calcul (suite)
• Opérateurs logiques:
Opérateur Signification
! Non
|| OU
&& ET

• Opérateurs d’incrémentation et décrémentation:


Opérateur Signification
++ Augmente d’une unité la variable
-- Diminue d’une unité la variable

11
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Saisie (Entrées)

•Instruction d’entrée: acquérir et placer une


valeur dans une variable.
scanf ("format",&variable) ;

•Exemple:
scanf ("%f",&Rayon) ;

Fonction de Format Adresse Identificateur


lecture de la variable

12
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Saisie (Entrées)

•Formats associés aux types:


Type Format

int ou short %d

long %D

float ou double %f

char %c

13
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Affichage (Sorties)
•Instruction de sortie: afficher sur l’écran (un
message, le contenu d’une variable ou le
résultat d’une expression)
printf (" Message format",variable ou expression) ;
• Exemple:
printf ("Le double de %d est %d ",R,2*R) ;

Message Variable
Format Expression

14
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives

• Opérateur conditionnel (1)


• Structure de choix simple (2)
• Structure de choix alternatif (3)
• Structure de choix multiples (4)

15
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(1) Opérateur conditionnel
Syntaxe: Test Test est faux
Test est vrai

(Expression logique) ? ExpressionSiVrai : ExpressionSiFaux


 L'expression conditionnelle débute par un test suivi du
caractère ? puis la valeur lorsque le test est vrai puis le caractère
: et enfin la valeur lorsque le test est faux.

Exemple:
renvoie le produit de i par 2 si i est strictement
positif et le résultat de l’ajout de 2 à i si i est
négatif ou nul.

(i>0) ? i*2 : i+2


16
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(1) Exercice d’application
Ecrire un programme en C qui permet de calculer la
valeur absolue de (A-B).

17
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives (suite)
(2) Structure de choix simple

if (conditions)
{
instruction 1;
instruction 2;

instruction n;
}

18
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(2) Exercice d’application
Ecrire un programme en C qui permet de calculer la
valeur absolue de (A-B).

19
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives (suite)
(3) Structure de choix alternatif

if (conditions)
{
instructions 1;
}
else
{
instructions 2;
}
20
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(3) Exercice d’application
Ecrire un programme en C qui permet de calculer la
valeur absolue de (A-B).

21
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives (suite)
(4) Structure de choix multiples
switch (variable)
{
case valeur 1: instructions 1;
break;

case valeur n: instructions n;
break;
default: instructions n+1;
}
22
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(4) Exercice d’application
Ecrire un programme en C qui affiche le jour
correspondant à un chiffre saisi entre 1 et 7.

23
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 1

Ecrire un programme en c qui affiche la


mention équivalente à une moyenne
obtenue à partir de résultats de trois
modules saisis au clavier. (chaque
module a une note et un coefficient)

24
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 1 (Solution)

25
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures itératives

• Boucle (1)
• Boucle (2)
• Boucle (3)

26
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures itératives (suite)
Boucle (1)
Initiations Conditions

for (expressions1; expressions2; expressions3)


{
instruction 1; Incrémentations
ou bien
instruction 2; Décrémentations

instruction n;
}

27
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(1) Exercice d’application
Afficher le mot « Bonjour » 100 fois.

(1)Initiation (2)Condition (3)Incrémentation

(4)Bloc à répéter

28
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 2

29
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 2 (Solution)

30
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures itératives (suite)
Boucle (2)

while (conditions)
{
instruction 1;
instruction 2;

instruction n;
}

31
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(2) Exercice d’application
Afficher le mot « Bonjour » 100 fois.

(1)Initiation
(2)Condition

(4)Bloc à répéter

(3)Incrémentation
32
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 3

Ecrire un programme en c qui calcule la


série suivante: S=1+2+3+…+N
Les étapes du programme sont:
- Lire un entier N
- Calculer S
- Afficher le résultat
33
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 3 (Solution)

34
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures itératives (suite)
Boucle (3)

do{
instruction 1;
instruction 2;

instruction n;
}while(conditions);

35
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Structures alternatives
(3) Exercice d’application
Afficher le mot « Bonjour » 100 fois.

(1)Initiation (4)Bloc à répéter

(3)Incrémentation

(2)Condition

36
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 4

Ecrire un programme en c qui calcule le


factoriel de N: N!=1×2×3×…×N
Les étapes du programme sont:
- Lire un entier N
- Calculer N!
- Afficher le résultat
37
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 4 (Solution)

38
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 5

Ecrire un programme en c qui affiche si un


entier saisi au clavier est un nombre parfait
ou non.

39
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 5 (Solution)

40
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 6

Ecrire un programme en c qui affiche les


nombres parfaits inférieurs strictement à
100.

41
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 7

Ecrire un programme en c qui affiche les


quatre premiers nombres parfaits.

42
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique
Exercice 8

Ecrire un programme en langage C qui


détermine la Nième valeur Un (N étant
fourni en donnée) de la suite de
« FIBONACCI » définie comme suit :
U1=1
U2=1
Un= Un-1+ Un-2 pour n>2

43
Prof: R. EL AYACHI ESTBM: GI Département d’Informatique

Vous aimerez peut-être aussi