C program to find all roots of a quadratic equation

Previous Program Next Program

Write a C program to find all roots of a quadratic equation using if else. How to find all roots of a quadratic equation using if else in C programming. Logic to calculate roots of quadratic equation in C program.

Example

Input

Input a: 8
Input b: -4
Input c: -2

Output

Root1: 0.80
Root2: -0.30

Required knowledge

Basic C programming, If else, Basic Mathematics

Logic to find roots of quadratic equation

This exercise is pure mathematics. You must be aware of quadratic equations and formula used to solve the quadratic equation. Here I am not going to get in depth of quadratic equations. I am just making a quick recap of senior secondary mathematics classes. Let me first talk about quadratic equation then we will move on to the logic section for this program.

Quadratic equation

Wikipedia states, in elementary algebra a quadratic equation is an equation in the form of
Quadratic equation = ax^2 + bx + c = 0

Solving quadratic equation

A quadratic equation can have either one or two distinct real or complex roots depending upon nature of discriminant of the equation. Where discriminant of the quadratic equation is given by

Discriminant formula = b^2 - 4ac

Depending upon the nature of the discriminant, formula for finding roots can be given as:

  • Case 1: If discriminant is positive. Then there are two real distinct roots given by:
    Real distinct roots of quadratic equation
  • Case 2: If discriminant is zero. Then it have exactly one real root given by:
    Single real root of quadratic equation
  • Case 3: If discriminant is negative. Then it will have two distinct complex roots given by:
    Distinct complex roots of quadratic equation

Based on the above learning let us write a step by step descriptive logic to find roots of a quadratic equation.

  1. Read three input from user. Store it in some variable say a, b and c.
  2. Find the discriminant of given equation. Use the above formula i.e. discriminant = (b*b) - (4*a*c).
  3. Compute the roots based on the nature of discriminant.
  4. Check if discriminant > 0. Then root1 = (-b + sqrt(discriminant)) / (2*a) and root2 = (-b - sqrt(discriminant)) / (2*a)
  5. Check if discriminant == 0. Then root1 = root2 = -b / (2*a).
  6. Check if discriminant < 0. Then there are two distinct complex roots where root1 = -b / (2*a) and root2 = -b / (2*a). Imaginary part is given by imaginary = sqrt(-discriminant) / (2*a);.

After this much reading let us finally code the solution of this program.

Program to find roots of quadratic equation

/**
 * C program to find all 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);
    
    discriminant = (b*b) - (4*a*c);
    
    
    /* Find the nature of discriminant */
    if(discriminant > 0)
    {
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);

        printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
    }
    else if(discriminant == 0)
    {
        root1 = root2 = -b / (2*a);

        printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
    }
    else if(discriminant < 0)
    {
        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);
    }

    return 0;
} 

Before you move on from this program to next exercise. It is recommended to learn this program using another approach - using switch cases.

Output
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 8 -4 -2
Two distinct and real roots exists: 0.81 and -0.31

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment