Home » C Program to find the roots of quadratic equation

C Program to find the roots of quadratic equation

by Online Tutorials Library

C Program to find the roots of quadratic equation

Quadratic equations are the polynomial equation with degree 2. It is represented as ax2 + bx +c = 0, where a, b and c are the coefficient variable of the equation. The universal rule of quadratic equation defines that the value of ‘a’ cannot be zero, and the value of x is used to find the roots of the quadratic equation (a, b). A quadratic equation’s roots are defined in three ways: real and distinct, real and equal, and real and imaginary.

C Program to find the roots of quadratic equation

Nature of the roots

The nature of the roots depends on the Discriminant (D) where D is.

  1. If D > 0, the roots are real and distinct (unequal)
  2. If D = 0, the roots are real and equal.
  3. If D < 0, the roots are real and imaginary.

Steps to find the square roots of the quadratic equation

  1. Initialize all the variables used in the quadratic equation.
  2. Take inputs of all coefficient variables x, y and z from the user.
  3. And then, find the discriminant of the quadratic equation using the formula:
    Discriminant = (y * y) – (4 * x *z).
  4. Calculate the roots based on the nature of the discriminant of the quadratic equation.
  5. If discriminant > 0, then
    Root1 = (-y + sqrt(det)) / (2 * x)
    Root2 = (-y + sqrt(det)) / (2 * x)
    Print the roots are real and distinct.
  6. Else if (discriminant = 0) then,
    Root1 = Root2 = -y / (2 * x).
    Print both roots are real and equal.
  7. Else (discriminant < 0), the roots are distinct complex where,
    Real part of the root is: Root1 = Root2 = -y / (2 * x) or real = -y / (2 * x).
    Imaginary part of the root is: sqrt( -discriminant) / (2 * x).
    Print both roots are imaginary, where first root is (r + i) img and second root is (r – i) img.
  8. Exit or terminate the program.

Pseudo Code of the Quadratic Equation

  1. Start
  2. Input the coefficient variable, x, y and z.
  3. D <- sqrt (y * y – 4 * x * z).
  4. R1 <- (-y + D) / ( 2 * x).
  5. R2 <- (-y – D) / (2 * x).
  6. Print the roots R1 and R2.
  7. Stop

Let’s implements the above steps in a C program to find the roots of the quadratic equation.

Output:

C Program to find the roots of quadratic equation

Let’s create another C program in which we have used function.

Output:

C Program to find the roots of quadratic equation


You may also like