Q:

C program to find the roots of a quadratic equation using a function

belongs to collection: C Programming on Numbers

0

C program to find the roots of a quadratic equation using a function

All Answers

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

#include <stdio.h>
#include <math.h>
void printQuadraticRoots(float a, float b,float c)
{
    float root1, root2, imaginary;
    float discriminant;
    // 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);
    }
}
int main()
{
    float a, b, c;
    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);
    printQuadraticRoots(a,b,c);
    return 0;
}

Outout:

Enter value of ‘a’ of quadratic equation (aX^2 + bX + c): 5
Enter value of ‘b’ of quadratic equation (aX^2 + bX + c): 2
Enter values of ‘c’ of quadratic equation (aX^2 + bX + c): 2
Two distinct complex roots exists: -0.20 + i0.60 and -0.20 – i0.60

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
C Program to Find the Roots of a Quadratic Equatio... >>
<< C program to find all roots of a quadratic equatio...