Q:

C Program to Find the Roots of a Quadratic Equation using if-else

belongs to collection: C Programming on Numbers

0

C Program to Find the Roots of a Quadratic Equation using if-else

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The below program ask the user to enter the value of a,b and c. After getting the value from the user it will calculate on the basis of ‘Discriminant’ value.

#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c;
    float root1, root2, imaginary;
    float discriminant;
    printf("Enter value of a of quadratic equation (aX^2 + bX + c): ");
    scanf("%f", &a);
    printf("Enter value of b of quadratic equation (aX^2 + bX + c): ");
    scanf("%f",&b);
    printf("Enter values of c of quadratic equation (aX^2 + bX + c): ");
    scanf("%f",&c);
    // Find discriminant of the equation
    discriminant = (b * b) - (4 * a * c);
    //Check different cases for the discriminant
    if(discriminant > 0)
    {
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);
        printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
    }
    else if(discriminant == 0)
    {
        root1 = root2 = -b / (2 * a);
        printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
    }
    else if(discriminant < 0)
    {
        root1 = root2 = -b / (2 * a);
        imaginary = sqrt(-discriminant) / (2 * a);
        printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
               root1, imaginary, root2, imaginary);
    }
    return 0;
}

Output:

Enter value of a of quadratic equation (aX^2 + bX + c): 2
Enter value of b of quadratic equation (aX^2 + bX + c): 7
Enter values of c of quadratic equation (aX^2 + bX + c): 2
Two distinct and real roots exist: -0.31 and -3.19

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Find Perfect Number using the function... >>
<< C program to find the roots of a quadratic equatio...