Finding Roots of Quadratic Equation

CCBeginner
Practice Now

Introduction

A quadratic equation is an equation of the form ax²+bx+c=0, where a, b, and c are constants. In this lab, we will learn how to write a program to find the roots of a quadratic equation.

To solve the quadratic equation, we will use the following formula:

x = (-b ± sqrt(b² - 4ac)) / 2a

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/comments -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/variables -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/data_types -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/operators -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/if_else -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/user_input -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/memory_address -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/pointers -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/function_declaration -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} c/math_functions -.-> lab-123254{{"`Finding Roots of Quadratic Equation`"}} end

Get Input Coefficients

We will take input from the user for coefficients of the quadratic equation such as a, b, and c. These coefficients will be used to calculate the roots of the quadratic equation.

float a, b, c, determinant, r1, r2, real, imag;
printf("\nEnter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);

Calculate the Determinant

We will then calculate the value of the determinant of the quadratic equation using the formula:

determinant = b*b - 4*a*c;

determinant = b*b - 4*a*c;

Check the Nature of Roots

We will then check the nature of the roots of the quadratic equation based on the value of determinant calculated in step 2. If the determinant is greater than 0, then the roots are real and distinct. If the determinant is equal to 0, then the roots are real and equal. If the determinant is less than 0, then the roots are complex and imaginary.

if(determinant > 0)    // both roots are real
{
    r1 = (-b + sqrt(determinant))/2*a;  // Brackets are important
    r2 = (-b - sqrt(determinant))/2*a;
    printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
else if(determinant == 0)   // both roots are real and equal
{
    r1 = r2 = -b/(2*a); // brackets are important
    printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
/*
    Determinant < 0 - both roots are imaginary of the
    form real + i*imaginary
*/
else
{
    real = -b/(2*a);
    imag = sqrt(-determinant)/(2*a);
    printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
}

Output the Roots

Finally, we will output the roots of the quadratic equation based on the nature of the roots calculated in Step 3.

Write the Full Code

#include<stdio.h>
#include<math.h>  // This is needed to use sqrt() function

int main()
{
    float a, b, c, determinant, r1, r2, real, imag;
    printf("\nEnter coefficients a, b and c: ");
    scanf("%f%f%f", &a, &b, &c);

    determinant = b*b - 4*a*c;

    if(determinant > 0)    // both roots are real
    {
        r1 = (-b + sqrt(determinant))/2*a;  // Brackets are important
        r2 = (-b - sqrt(determinant))/2*a;
        printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
    }
    else if(determinant == 0)   // both roots are real and equal
    {
        r1 = r2 = -b/(2*a); // brackets are important
        printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
    }
    /*
        Determinant < 0 - both roots are imaginary of the
        form real + i*imaginary
    */
    else
    {
        real = -b/(2*a);
        imag = sqrt(-determinant)/(2*a);
        printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
    }
    printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Summary

In this lab, we learned how to write a C program to find the roots of a quadratic equation. We took input from the user for coefficients of the quadratic equation and calculated the roots based on the nature of the roots determined by the value of determinant. We then output the roots of the quadratic equation based on the nature of the roots.

Other C Tutorials you may like