C program quadratic equation

devquora
devquora

Posted On: Dec 23, 2020

C program quadratic equation

 

Overview:

Quadratic equation is a polynomial equation. In this article I will demonstrate the standard form of the quadratic equation. After all, I will write the logic and C program quadratic equation with output.

Table of contents:

  1. What is a quadratic equation?
  2. Demonstration or standard form of quadratic equation
  3. Logic to find roots of a quadratic equation
  4. C program to find roots of a quadratic equation
  5. Explanation of the C program find roots of a quadratic equation with output.
  6. Conclusion

What is a quadratic equation?

A second order polynomial equation of type ax2+bx+c=0, where, x is a variable and a≠0 is known as a Quadratic Equation.

Demonstration or standard form of quadratic equation

The standard form of a quadratic equation is:

ax2 + bx + c = 0

where, a, b and c are real numbers and a != 0

The term b2-4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.

If the discriminant is greater than 0, the roots are real and different.

If the discriminant is equal to 0, the roots are real and equal.

If the discriminant is less than 0, the roots are complex and different.

quadratic roots

Logic to find roots of a quadratic equation

  • Step 1: Input coefficients of quadratic equation. Store it in some variable say a, b and c.
  • Step 2: Find discriminant of given equation using formula i.e. discriminant = (b * b) - (4 * a * c).
  • Step 3: You can also use pow() function to find square of b.
  • Step 4: Compute the roots based on the nature of discriminant. Switch the value of switch(discriminant > 0).
  • Step 5: The expression (discriminant > 0) can have two possible cases i.e. case 0 and case 1.
  • Step 6: For case 1 means discriminant is positive. Apply formula root1 = (-b + sqrt(discriminant)) / (2*a); to compute root1 and root2 = (-b - sqrt(discriminant)) / (2*a); to compute root2.
  • Step 7: For case 0 means discriminant is either negative or zero. There exist one more condition to check i.e. switch(discriminant < 0).
  • Step 8: Inside case 0 switch the expression switch(discriminant < 0).
  • Step 9: For the above nested switch there are two possible cases. Which is case 1 and case 0. case 1 means discriminant is negative. Whereas case 0 means discriminant is zero.
  • Step 10: Apply the formula to compute roots for both the inner cases.

C program to find roots of a quadratic equation

#include <stdio.h>
#include <math.h>/* Used for sqrt() */

int main()
{
    float a, b, c;
    float root1, root2, imaginary;
    float discriminant;
    printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
    scanf("%f%f%f", &a, &b, &c);
    /* Calculate discriminant */
    discriminant = (b * b) - (4 * a * c);
    /* Compute roots of quadratic equation based on the nature of discriminant */
    switch(discriminant > 0)
    {
        case 1:
            /* If discriminant is positive */
            root1 = (-b + sqrt(discriminant)) / (2 * a);
            root2 = (-b - sqrt(discriminant)) / (2 * a);
            printf("Two distinct and real roots exists: %.2f and %.2f", 
                    root1, root2);
            break;
        case 0:
            /* If discriminant is not positive */
            switch(discriminant < 0)
            {
                case 1:
                    /* If discriminant is negative */
                    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);
                    break;
                case 0:
                    /* If discriminant is zero */
                    root1 = root2 = -b / (2 * a);
                    printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
                    break;
            }
    }
    return 0;
}

The output of the Program with code:

c program quadratic equation

Conclusion:

After the successful compilation of the program, this program displays the following output: Enter values for a, b, c of quadratic equation ax2+bx+c are: If user enters 1, 2, 6 then it displays two distinct complex roots exists: -1.00 + i2.24 and -1.00 - i2.24

    Related Questions

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..