CS Electrical And Electronics
@cselectricalandelectronics

Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.

All QuestionsCategory: C LanguageDevelop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
nikhil.d asked 3 years ago
2 Answers
nikhil.d answered 3 years ago

#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c, d, r1, r2;
    printf(“Enter non-zero coefficients”);
    scanf(“%f %f %f”, &a, &b, &c);
    d = (b * b) – 4 * a * c;
    if (d == 0)
    {
        r1 = r2 = (-b) / (2 * a);
        printf(“Root are equal\n”);
        printf(“Root1 = %f\n and Root2 = %f\n”, r1, r2);
    }
    else if (d > 0)
    {
        r1 = (-b + sqrt(d)) / (2 * a);
        r2 = (-b – sqrt(d)) / (2 * a);
        printf(“Roots are real and distinct\n”);
        printf(“Root1 = %f\n”, r1);
        printf(“Root2 = %f\n”, r2);
    }
    else
    {
        r1 = (-b) / (2 * a);
        r2 = sqrt(fabs(d)) / (2 * a);
        printf(“Roots are complex\n”);
        printf(“Root1 = %f+i%f \n”, r1, r2);
        printf(“Root2 = %f-i%f \n”, r1, r2);
    }
}

Anonymous answered 3 years ago

Note: Change the double inverted comma ( ” ) in the code to avoid an error.