From ea050f9ca53965ec67f89458ab998f2d031f17e4 Mon Sep 17 00:00:00 2001 From: rutujabhagane <73490304+rutujabhagane@users.noreply.github.com> Date: Sat, 31 Oct 2020 20:37:48 +0530 Subject: [PATCH] Program Program to Find Roots of a Quadratic Equation --- Quadratic Equation | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Quadratic Equation diff --git a/Quadratic Equation b/Quadratic Equation new file mode 100644 index 00000000..272efe95 --- /dev/null +++ b/Quadratic Equation @@ -0,0 +1,32 @@ +#include +#include +int main() { + double a, b, c, discriminant, root1, root2, realPart, imagPart; + printf("Enter coefficients a, b and c: "); + scanf("%lf %lf %lf", &a, &b, &c); + + discriminant = b * b - 4 * a * c; + + // condition for real and different roots + if (discriminant > 0) { + root1 = (-b + sqrt(discriminant)) / (2 * a); + root2 = (-b - sqrt(discriminant)) / (2 * a); + printf("root1 = %.2lf and root2 = %.2lf", root1, root2); + } + + // condition for real and equal roots + else if (discriminant == 0) { + root1 = root2 = -b / (2 * a); + printf("root1 = root2 = %.2lf;", root1); + } + + // if roots are not real + else { + realPart = -b / (2 * a); + imagPart = sqrt(-discriminant) / (2 * a); + printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); + } + + return 0; +} +