Write a Program to Find the Roots of a Quadratic Equation in C

Write a Program to Find the Roots of a Quadratic Equation in C

Quadratic equations are fundamental in algebra and can model a variety of real-world situations. A quadratic equation is typically in the form ax² + bx + c = 0, where a, b, and c are constants. The solutions to this equation are known as the roots and can be found using the quadratic formula:

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

In this blog post, we will write a C program to calculate the roots of a quadratic equation using this formula, discuss how it works, and explore potential edge cases.

1. Understanding the Quadratic Formula

The quadratic formula allows us to find the roots of any quadratic equation, regardless of whether the roots are real or complex:

  • If b² - 4ac > 0, there are two distinct real roots.
  • If b² - 4ac = 0, there is one real root (a repeated root).
  • If b² - 4ac < 0, the roots are complex (not real).

2. Writing the Program

Below is the C program to find the roots of a quadratic equation:

Program to Find Roots of a Quadratic Equation

#include <stdio.h>
#include <math.h> // For sqrt function

int main() {
    float a, b, c; // Coefficients of the quadratic equation
    float discriminant, root1, root2, realPart, imaginaryPart;

    // Input coefficients
    printf("Enter coefficients a, b, and c: ");
    scanf("%f %f %f", &a, &b, &c);

    // Calculate the discriminant
    discriminant = b * b - 4 * a * c;

    // Check if a is zero
    if (a == 0) {
        printf("Invalid input: 'a' cannot be zero.\n");
        return 1; // Exit the program
    }

    // Calculate roots based on the discriminant
    if (discriminant > 0) {
        // Two distinct real roots
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);
        printf("Roots are real and different.\n");
        printf("Root 1 = %.2f\n", root1);
        printf("Root 2 = %.2f\n", root2);
    } else if (discriminant == 0) {
        // One real root
        root1 = -b / (2 * a);
        printf("Roots are real and the same.\n");
        printf("Root 1 = Root 2 = %.2f\n", root1);
    } else {
        // Complex roots
        realPart = -b / (2 * a);
        imaginaryPart = sqrt(-discriminant) / (2 * a);
        printf("Roots are complex and different.\n");
        printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);
        printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);
    }

    return 0;
}

Explanation of the Code

Let’s break down the program step-by-step:

  • We start by including the necessary header files: stdio.h for input/output functions and math.h for the square root function.
  • The user is prompted to enter the coefficients a, b, and c.
  • We calculate the discriminant using the formula b² - 4ac.
  • Depending on the value of the discriminant, we calculate the roots accordingly and display the results:
    • If the discriminant is greater than zero, we compute two distinct real roots.
    • If the discriminant is zero, we compute one real root (double root).
    • If the discriminant is less than zero, we compute the complex roots.

3. Example Output

Here’s an example of how the program works:

Enter coefficients a, b, and c: 1 -3 2
Roots are real and different.
Root 1 = 2.00
Root 2 = 1.00

4. Common Mistakes to Avoid

  • Division by Zero: Ensure that a is not zero, as this would not represent a quadratic equation.
  • Wrong Input Types: Ensure that the input values are numeric. Non-numeric input can cause runtime errors.

5. Conclusion

In this post, we have created a C program to find the roots of a quadratic equation using the quadratic formula. This knowledge is essential for solving various mathematical problems and can be applied in many fields, such as physics and engineering. By understanding how to manipulate equations programmatically, you’ll gain a deeper appreciation for the mathematical foundations that underlie programming.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post