Finding the roots of a quadratic equation isn’t as tough as it seems. Take the equation ax² + bx + c = 0. You’ll run into this a lot in exams or real-life stuff. Let’s check out how to figure them out using C.
Here’s a quick example — for the equation x² + 5x + 6 = 0, the roots are -2 and -3. Now, let’s jump into the code that makes this happen.
Prerequisites for Finding the Roots Program in C
Before coding, make sure you know the following:
- Understanding of quadratic equations and the quadratic formula
- Using math.h for functions like sqrt
- Basic if...else conditional statements
- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Find the Roots of a Quadratic Equation
Here's the complete code to find the roots of a quadratic equation using the quadratic formula. It's a great example of conditional logic in action.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
// Accept coefficients from the user
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate the discriminant
discriminant = b*b - 4*a*c;
// Check the nature of the discriminant
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Root1 = %.2f\n", root1);
printf("Root2 = %.2f\n", root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2*a);
printf("Root1 = Root2 = %.2f\n", root1);
}
else {
realPart = -b / (2*a);
imaginaryPart = sqrt(-discriminant) / (2*a);
printf("Root1 = %.2f + %.2fi\n", realPart, imaginaryPart);
printf("Root2 = %.2f - %.2fi\n", realPart, imaginaryPart);
}
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Find the Roots Program
Here's what you'll see when you run the program. We used the coefficients 1, 5, and 6 in this example.
Enter coefficients a, b and c: 1 5 6
Root1 = -2.00
Root2 = -3.00
Algorithm to Find the Roots of a Quadratic Equation in C
Finding the roots involves calculating the discriminant and using the quadratic formula. Here is the exact logic, step by step.
- Read the coefficients a, b, and c from the user.
- Calculate the discriminant using the formula
d = b*b - 4*a*c. - If the discriminant is greater than 0, calculate two distinct real roots using the formulas.
- If the discriminant is 0, calculate one real root.
- If the discriminant is less than 0, calculate the real and imaginary parts of the complex roots.
- Display the roots accordingly.
flowchart TD
A([Start]) --> B[/Read coefficients a, b, c/]
B --> C[Calculate discriminant (d)]
C --> D{"d > 0?"}
D -->|Yes| E[Two real & distinct roots]
E --> F["root1 = (-b + sqrt(d)) / (2*a)"]
F --> G["root2 = (-b - sqrt(d)) / (2*a)"]
G --> H[/Display root1 and root2/]
D -->|No| I{"d == 0?"}
I -->|Yes| J["root1 = root2 = -b / (2*a)"]
J --> K[/Display root1/]
I -->|No| L["Two complex roots"]
L --> M[Calculate real and imaginary parts]
M --> N[/Display complex roots/]
H --> O([End])
K --> O
N --> OHow the C Program to Find Roots Works
Let's break down the code line by line to understand how it works.
#include <stdio.h>— Imports the standard input/output library forprintfandscanf.#include <math.h>— Imports the math library for mathematical functions likesqrt.int main()— Main function where execution begins.float a, b, c, ...— Declare variables to store coefficients and roots.scanf("%f %f %f", &a, &b, &c)— Reads input for coefficientsa,b, andc.discriminant = b*b - 4*a*c— Calculates the discriminant to decide the nature of the roots.if (discriminant > 0)— Conditional check for two real and distinct roots.root1 = (-b + sqrt(discriminant)) / (2*a)— Calculates the first root.root2 = (-b - sqrt(discriminant)) / (2*a)— Calculates the second root.else if (discriminant == 0)— Checks for a single real root.root1 = root2 = -b / (2*a)— Single real root.else— Handles complex roots with real and imaginary parts.return 0— Ends the program.
Quick Quiz — test your understanding
Tap an option to check your answerWhat does the discriminant d determine in this program?
Time and Space Complexity of the Quadratic Equation Program
Understanding the efficiency of this program is essential.
Time Complexity: O(1). The operations are fixed and do not depend on the size of input, making the execution time constant.
Space Complexity: O(1). The program uses a constant amount of memory, storing only a fixed number of variables like a, b, c.
Common mistake: Forgetting to include math.h can cause errors when trying to use sqrt().
Key takeaway: This program shows how important the discriminant is for figuring out the nature of quadratic roots.
FAQs About Finding Roots of Quadratic Equations in C
Here are some common questions about this program.
math.h gives you the sqrt() function, which is key for calculating square roots.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Calculate Difference Between Two Time Periods
- C Program to Check Leap Year with Code and Output
- C Program to Find the Area and Perimeter of a Rectangle
- C Program to Calculate Area and Circumference of Circle
- C Program to Calculate Standard Deviation — Full Code & Output
Conclusion
Got quadratic equations to solve? This program makes it a breeze. You’ll find this approach useful in plenty of math applications.
Want to explore more? Check out our C programming quiz or run your own tests in the online compiler.