Trang

Monday, March 31, 2014

Giải phương trình bậc 2

Bài tập: Giải phương trình bậc 2

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
    float a,b,c;
    float x1,x2;
    float delta;
    printf("Nhap 3 he so a, b, c : ");
    scanf("%f%f%f",&a,&b,&c);
    if(a!=0){
        delta=b*b-4*a*c;
        if(delta>0){
            x1=(-b+sqrt(delta))/(2*a);
            x2=(-b-sqrt(delta))/(2*a);
            printf("\nPhuong trinh co 2 nghiem phan biet : \n");
            printf("x1 = %.2f\nx2 = %.2f",x1,x2);
        }
        else if(delta==0){
            x1=-b/(2*a);
            printf("\nphuong trinh co nghiem kep : ");
            printf("\nx1 = x2 = %.2f",x1);
        }
        else
            printf("\nphuong trinh vo nghiem !");
    }
    else
    printf("\nphuong trinh tro ve bac nhat");

    getch();
    return 0;
}

No comments:

Post a Comment