C Program to Calculate the Roots of a Quadratic Equation - Tutcoach (2024)

Solving a quadratic equation is a common problem in mathematics and programming. A quadratic equation is of the form of

ax^2 + bx + c = 0

The solution to the quadratic equation is given by the Quadratic Formula.

x = \frac{{-b \pm \sqrt{{b^2 - 4ac}}}}{2a}

The roots of this equation can be found using various methods. This article will cover different C Program to Calculate the Roots of a Quadratic Equation. We will explore multiple examples, each demonstrating a different solution. Additionally, we will discuss the prerequisites, provide detailed explanations for each example, and conclude with a summary of what we’ve learned.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, especially conditionals, functions, and mathematical operations

1. Finding the Roots of a Quadratic Equation

In this section, we will look at several methods to find the roots of a quadratic equation in C.

1.1 Using the Standard Formula

Example 1: Calculate the Roots Using the Standard Formula

The standard formula to find the roots of a quadratic equation is shown above

Code

C

#include <stdio.h>#include <math.h>int main() { double a, b, c, discriminant, root1, root2; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c; if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and different.\n"); printf("Root 1 = %.2lf\n", root1); printf("Root 2 = %.2lf\n", root2); } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("Roots are real and the same.\n"); printf("Root 1 = Root 2 = %.2lf\n", root1); } else { double realPart = -b / (2 * a); double imaginaryPart = sqrt(-discriminant) / (2 * a); printf("Roots are complex and different.\n"); printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart); printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart); } return 0;}

Explanation

  • Include necessary headers: #include <stdio.h> for input/output functions and #include <math.h> for mathematical functions.
  • Input the coefficients: scanf reads the coefficients aaa, bbb, and ccc from the user.
  • Calculate the discriminant: The discriminant is calculated as b2−4acb^2 – 4acb2−4ac.
  • Check the nature of the roots: Based on the discriminant, determine if the roots are real and different, real and the same, or complex and different.
  • Calculate and output the roots: Use the standard formula to calculate the roots and display them.

Output

C

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

1.2 Using a Function

Example 2: Calculate the Roots Using a Function

This method encapsulates the root calculation logic in a separate function for better modularity.

Code

C

#include <stdio.h>#include <math.h>void findRoots(double a, double b, double c);int main() { double a, b, c; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); findRoots(a, b, c); return 0;}void findRoots(double a, double b, double c) { double discriminant, root1, root2; discriminant = b * b - 4 * a * c; if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and different.\n"); printf("Root 1 = %.2lf\n", root1); printf("Root 2 = %.2lf\n", root2); } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("Roots are real and the same.\n"); printf("Root 1 = Root 2 = %.2lf\n", root1); } else { double realPart = -b / (2 * a); double imaginaryPart = sqrt(-discriminant) / (2 * a); printf("Roots are complex and different.\n"); printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart); printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart); }}

Explanation

  • Include necessary headers: #include <stdio.h> for input/output functions and #include <math.h> for mathematical functions.
  • Declare a function: void findRoots(double a, double b, double c) to encapsulate the root calculation logic.
  • Input the coefficients: scanf reads the coefficients aaa, bbb, and ccc from the user.
  • Call the function: findRoots(a, b, c) determines the nature and values of the roots and displays them.

Output

C

Enter coefficients a, b and c: 1 2 1Roots are real and the same.Root 1 = Root 2 = -1.00

1.3 Handling Complex Roots

Example 3: Calculate and Display Complex Roots

This method specifically handles the case where the roots are complex.

Code

C

#include <stdio.h>#include <math.h>int main() { double a, b, c, discriminant, realPart, imaginaryPart; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c; if (discriminant > 0) { double root1 = (-b + sqrt(discriminant)) / (2 * a); double root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and different.\n"); printf("Root 1 = %.2lf\n", root1); printf("Root 2 = %.2lf\n", root2); } else if (discriminant == 0) { double root = -b / (2 * a); printf("Roots are real and the same.\n"); printf("Root 1 = Root 2 = %.2lf\n", root); } else { realPart = -b / (2 * a); imaginaryPart = sqrt(-discriminant) / (2 * a); printf("Roots are complex and different.\n"); printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart); printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart); } return 0;}

Explanation

  • Include necessary headers: #include <stdio.h> for input/output functions and #include <math.h> for mathematical functions.
  • Input the coefficients: scanf reads the coefficients aaa, bbb, and ccc from the user.
  • Calculate the discriminant: The discriminant is calculated as b2−4acb^2 – 4acb2−4ac.
  • Check the nature of the roots and handle complex roots: Based on the discriminant, determine if the roots are real or complex and display them accordingly.

Output

C

Enter coefficients a, b and c: 1 1 1Roots are complex and different.Root 1 = -0.50 + 0.87iRoot 2 = -0.50 - 0.87i

1.4 Using a Struct to Return Roots

Example 4: Using a Struct to Return Roots

This method uses a struct to return the roots and their nature.

Code

C

#include <stdio.h>#include <math.h>typedef struct { double root1; double root2; int isComplex;} Roots;Roots findRoots(double a, double b, double c);int main() { double a, b, c; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); Roots roots = findRoots(a, b, c); if (roots.isComplex) { printf("Roots are complex and different.\n"); printf("Root 1 = %.2lf + %.2lfi\n", roots.root1, roots.root2); printf("Root 2 = %.2lf - %.2lfi\n", roots.root1, roots.root2); } else if (roots.root1 == roots.root2) { printf("Roots are real and the same.\n"); printf("Root 1 = Root 2 = %.2lf\n", roots.root1); } else { printf("Roots are real and different.\n"); printf("Root 1 = %.2lf\n", roots.root1); printf("Root 2 = %.2lf\n", roots.root2); } return 0;}Roots findRoots(double a, double b, double c) { Roots r; double discriminant = b * b - 4 * a * c; if (discriminant > 0) { r.root1 = (-b + sqrt(discriminant)) / (2 * a); r.root2 = (-b - sqrt(discriminant)) / (2 * a); r.isComplex = 0; } else if (discriminant == 0) { r.root1 = r.root2 = -b / (2 * a); r.isComplex = 0; } else { r.root1 = -b / (2 * a); r.root2 = sqrt(-discriminant) / (2 * a); r.isComplex = 1; } return r;}

Explanation

  • Include necessary headers: #include <stdio.h> for input/output functions and #include <math.h> for mathematical functions.
  • Declare a struct: typedef struct { ... } Roots to encapsulate the roots and their nature.
  • Declare a function: Roots findRoots(double a, double b, double c) to calculate the roots and return them as a struct.
  • Input the coefficients: scanf reads the coefficients aaa, bbb, and ccc from the user.
  • Call the function: findRoots(a, b, c) determines the nature and values of the roots and returns them as a struct.
  • Output the result: Based on the struct values, display the roots.

Output

C

Enter coefficients a, b and c: 1 2 5Roots are complex and different.Root 1 = -1.00 + 2.00iRoot 2 = -1.00 - 2.00i

2. Conclusion

In this article, we explored various methods to find the roots of a quadratic equation in C: using the standard formula, using a function, handling complex roots, and using a struct to return roots. Each method demonstrates different aspects of handling mathematical operations and organizing code in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in solving quadratic equations and modular programming in C.

Using these examples as a guide, you can confidently calculate the roots of quadratic equations in various ways depending on your requirements, making your programs more flexible and efficient.

C Program to Calculate the Roots of a Quadratic Equation - Tutcoach (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6260

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.