c programmation

Embed Size (px)

Citation preview

  • 7/30/2019 c programmation

    1/4

    Exercice 1

    crire une fonction prenant en argument un prix hors taxe et calculant le prix TTC.Correction :

    # include

    float tax(float n) {

    return 1.206 * n ;

    }

    int main() {

    float n ;

    printf("Prix HT ? ") ;

    scanf("%f",&n) ;

    printf("TTC : %.2f\n",tax(n)) ;

    return 0 ;}

    Exercice 2

    crire une fonction prenant en entre : heure, minute et secondes et renvoyant le nombre desecondes.Correction :

    # include

    int hms(int h,int m,int s) {

    return s + 60*m + 3600 * h ;

    }

    int main() {

    int x,y,z ;

    printf("Heure Minute Seconde : ") ;

    scanf("%d%d%d",&x,&y,&z) ;

    printf("Secondes : %d\n",hms(x,y,z)) ;

    return 0 ;}

    Exercice 3

    crire une fonction prenant en argument un entiern et imprimant un triangle de * d'unelargeurn.*

    *****

    ****

  • 7/30/2019 c programmation

    2/4

    *****

    ******

    *******

    ********

    *********

    **********

    Correction :

    # include

    # define LARGEUR 10

    void triangle(int n) {

    int i,j ;

    for(i=1;i

  • 7/30/2019 c programmation

    3/4

    printf("\n") ;

    }

    }

    int main() {

    triangle2(LARGEUR) ;

    return 0 ;}

    Exercice 5

    Modifier un des programmes prcdents pour avoir le triangle ci-dessous.**********

    *********

    ********

    *************

    *****

    ****

    ***

    **

    *

    Correction :

    # include

    # define LARGEUR 10

    void triangle3(int n) {

    int i,j ;

    for(i=1;i

  • 7/30/2019 c programmation

    4/4

    Correction :

    # include

    int calc(int x,char op,int y) {

    switch(op) {case '+' : return x+y ;

    case '-' : return x-y ;

    case '*' : return x*y ;

    case '/' :

    if(y != 0)

    return x/y ;

    printf("Division par 0.\n") ;

    exit(-2) ;

    default:

    printf("%c n'est pas un operateur\n.",op) ;

    exit(-1) ;}

    }

    int main() {

    int x,y ;

    char op ;

    printf("X ? Y : ") ;

    scanf("%d %c %d",&x,&op,&y) ;

    printf("Res : %d\n",calc(x,op,y)) ;

    return 0 ;

    }