0% found this document useful (0 votes)
18 views2 pages

Solve Quadratic Equation

Quadratic equation solver Program
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Solve Quadratic Equation

Quadratic equation solver Program
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

#include <math.h>

// Hàm giải phương trình bậc hai ax^2 + bx + c = 0

void solveQuadraticEquation(float a, float b, float c) {

float discriminant, root1, root2, realPart, imaginaryPart;

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

// Hai nghiệm thực phân biệt

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Root 1 = %.2f\n", root1);

printf("Root 2 = %.2f\n", root2);

} else if (discriminant == 0) {

// Hai nghiệm thực trùng nhau

root1 = -b / (2 * a);

printf("Root 1 = Root 2 = %.2f\n", root1);

} else {

// Hai nghiệm phức

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);

printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);


}

int main() {

float a, b, c;

printf("Enter coefficients a, b and c: ");

scanf("%f %f %f", &a, &b, &c);

if (a == 0) {

printf("Coefficient a cannot be 0.\n");

} else {

solveQuadraticEquation(a, b, c);

return 0;

You might also like