C Program to Find out the Roots of a Quadratic Equation

«
»

This is a C Program to find out the roots of a quadratic equation.

Problem Description

This C Program calculates the roots of a quadratic equation.

Problem Solution

First it finds discriminant using the formula : disc = b * b – 4 * a * c. There are 3 types of roots. They are complex, distinct & equal roots. We have to find the given equation belongs to which type of root.

advertisement
Program/Source Code

Here is source code of the C program to calculate the roots of a quadratic equation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to find out the roots of a quadratic equation
 * for non-zero coefficients. In case of errors the program
 * should report suitable error message.
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
void main()
{
    float a, b, c, root1, root2;
    float realp, imagp, disc;
 
    printf("Enter the values of a, b and c \n");
    scanf("%f %f %f", &a, &b, &c);
    /* If a = 0, it is not a quadratic equation */
    if (a == 0 || b == 0 || c == 0)
    {
        printf("Error: Roots cannot be determined \n");
        exit(1);
    }
    else
    {
        disc = b * b - 4.0 * a * c;
        if (disc < 0)
        {
            printf("Imaginary Roots\n");
            realp = -b / (2.0 * a) ;
            imagp = sqrt(abs(disc)) / (2.0 * a);
            printf("Root1 = %f  +i %f\n", realp, imagp);
            printf("Root2 = %f  -i %f\n", realp, imagp);
        }
        else if (disc == 0)
        {
            printf("Roots are real and equal\n");
            root1 = -b / (2.0 * a);
            root2 = root1;
            printf("Root1 = %f\n", root1);
            printf("Root2 = %f\n", root2);
        }
        else if (disc > 0 )
        {
            printf("Roots are real and distinct \n");
            root1 =(-b + sqrt(disc)) / (2.0 * a);
            root2 =(-b - sqrt(disc)) / (2.0 * a);
            printf("Root1 = %f  \n", root1);
            printf("Root2 = %f  \n", root2);
        }
    }
}
Program Explanation

In this C program, we are reading three integer values using ‘a’, ’b’ and ‘c’ variables. If else condition statement is used to check the values are equal to 0. If the condition is true, then it is not a quadratic equation execute the statement and print as Error: Roots cannot be determined.

Otherwise, if the condition is false, then execute the else statement. To find the discriminant value, the following formula is used

advertisement

Disc = b * b – 4 * a * c.

Nested if else condition statement is used to display the 3 types of roots they are complex, distinct & equal roots from the equation.

If the equation value in ‘disc’ variable is less than 0, then it is imaginary roots, execute the statement. To compute the real part and imaginary part the following formula is used

Real part = -b / (2.0* a)
Imaginary part = sqrt (abs (disc))/ (2.0* a)

advertisement

Otherwise, if the condition is false, then execute the elseif condition statement. Check the value of ‘disc’ variable is equal to 0. If the condition is true, then roots are real and equal, execute the statement. To compute the real part and imaginary part the following formula is used

Real part = -b / (2.0* a)
Real part = Imaginary part

If both the condition statements is false, then execute another else if statement. Check the value of ‘disc’ variable is greater than 0. If the condition is true, then the roots are real and distinct, execute the statement. To compute the real part and imaginary part the following formula is used

Real part = (-b +sqrt(disc))/(2.0* a)
Imaginary part = (-b -sqrt(disc))/(2.0* a)

advertisement
Runtime Test Cases
 
$ cc pgm7.c -lm
$ a.out
Enter the values of a, b and c
45 50 65
Imaginary Roots
Root1 = -0.555556  +i 1.065740
Root2 = -0.555556  -i 1.065740

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn