Compute the Roots of Quadratic Equation - C Data Type

C examples for Data Type:float

Introduction

To compute the roots of the quadratic equation ax^2 + bx + c = 0.

These roots are given by the following formula:

(-b + square_root(b2 - 4ac))/2a        and      (-b - square_root(b2 - 4ac))/2a

Demo Code

#include <stdio.h>
#include <math.h>

int main(){/*  w w  w  . j  a v a 2 s .c  om*/
    double a, b, c, d, result1, result2;
    char ch;

    printf("Enter the values of a, b and c : ");
    scanf("%lf %lf %lf", &a, &b, &c);

    d = b * b - 4 * a * c;
    if (d == 0)
    {
        result1 = ( - b) / (2 * a);
        result2 = result1;
        printf("Roots are real & equal\n");
        printf("Root1 = %f, Root2 = %f\n", result1, result2);
    }
    else if (d > 0)
    {
        result1 =  - (b + sqrt(d)) / (2 * a);
        result2 =  - (b - sqrt(d)) / (2 * a);
        printf("Roots are real & distinct\n");
        printf("Root1 = %f, Root2 = %f\n", result1, result2);
    }
    else
    {
        printf("Roots are imaginary\n");
    }

    return 0;
}

Result


Related Tutorials