C program to find roots of a quadratic equation

Find Roots of a Quadratic Equation

C program to find roots of a quadratic equation

The mathematical representation of a Quadratic Equation is ax²+bx+c = 0. If discriminant > 0 then Two Distinct Real Roots will exist for this equation.

Root1= (-b+ √b2 - 4ac) / 2a
Root2= (-b- √b2 - 4ac) / 2a

Today, we will learn to find the roots of a quadratic equation.

#include <stdio.h>
#include <math.h> /* Used for sqrt() */
void main()
{
    float a, b, c;
    float root1, root2, imaginary;
    float discriminant;
    clrscr();
printf("\n\t\t\t...Welcome To EduNews.Tech... ");
    printf("\n\nEnter values of a, b, c of quadratic equation : ");
    scanf("%f%f%f", &a, &b, &c);
    /* Find discriminant of the equation */
    discriminant = (b * b) - (4 * a * c);
    /* Find the nature of 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);

    }
    printf("\n\n\n\n\t\t\tThankyou for Joining Us !");
    printf("\n\t\t\t!Regards EduNews !");
    
getch();

}

Program Output:

quadratic equation

I hope this post helps you to understand roots of quadratic equation and its implementation in C programming language.

Keep coding 🙂

2 thoughts on “C program to find roots of a quadratic equation”

Leave a Reply

Your email address will not be published. Required fields are marked *