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

Quadratic Equation Roots Calculator

This C program calculates the roots of a quadratic equation based on user-provided coefficients a, b, and c. It determines the nature of the roots by calculating the discriminant and outputs either real and distinct roots, real and equal roots, or complex roots. The program uses the sqrt() function from the math library to compute square roots as needed.

Uploaded by

Netra Yardoni
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)
9 views2 pages

Quadratic Equation Roots Calculator

This C program calculates the roots of a quadratic equation based on user-provided coefficients a, b, and c. It determines the nature of the roots by calculating the discriminant and outputs either real and distinct roots, real and equal roots, or complex roots. The program uses the sqrt() function from the math library to compute square roots as needed.

Uploaded by

Netra Yardoni
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

#include <stdio.

h>

#include <math.h> // Required for sqrt() function

int main()

double a, b, c; // Coefficients of the quadratic equation

double discriminant, root1, root2, realPart, imagPart;

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

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

// Calculate the discriminant

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

// Check the nature of the roots based on the discriminant

if (discriminant > 0) {

// Real and distinct roots

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

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

printf("Roots are real and distinct:\n");

printf("root1 = %.2lf\n", root1);

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

} else if (discriminant == 0) {

// Real and equal roots

root1 = root2 = -b / (2 * a);


printf("Roots are real and equal:\n");

printf("root1 = root2 = %.2lf\n", root1);

} else {

// Complex roots

realPart = -b / (2 * a);

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

printf("Roots are complex and distinct:\n");

printf("root1 = %.2lf + %.2lfi\n", realPart, imagPart);

printf("root2 = %.2lf - %.2lfi\n", realPart, imagPart);

return 0;

You might also like