Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# C Program to Find Roots of Quadratic Equation

Last Updated on May 10, 2023 by Prepbytes

This article will go over a C program for finding the roots of quadratic equations in depth. Finding the roots of the quadratic equation in c will help in building the logic, which will enhance your coding career. Let’s talk about what a quadratic equation is and how to use a C program to find the roots of a quadratic equation. To begin, what exactly is a quadratic equation?

## What is a Quadratic Equation in C?

A quadratic equation is an equation of degree 2, which means that the function’s highest exponent is 2. A quadratic has the standard form y = ax2 + bx + c, where a, b, and c are numbers and a cannot be zero. Example of a quadratic equation: 3x^2 + 3x + 1. Let’s look at what quadratic equation roots are and how to get them from the equation.

The term b^2 – 4ac is called the discriminant of a quadratic equation. It tells the nature of the roots.

• If the discriminant is greater than 0, the roots are real and different.
• If the discriminant is equal to 0, the roots are real and equal.
• If the discriminant is less than 0, the roots are complex and different.

### Algorithm to Find the Roots of Quadratic Equations in C

1. Input the value of a, b, c.
2. Calculate k = bb – 4a*c
3. If (d < 0)
• Print "Roots are Imaginary, calculate root1 = (-b +i ?k)/ 2a and root2 =(b + i?k)/ 2a.
4. else if (d = 0)
• Print "Roots are Equal" and calculate root1 = root2 = (-b / 2*a)
5. else
• Print "Roots are real and calculate root1 = -b + ?d / 2a and root2 = -b – ?d / 2a.
6. Print root1 and root2.
7. End the algorithm.

### Code Implementation to Find Roots of Quadratic Equations in C

```#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
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("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}
```

Output

``root1 = -1.00+1.41i and root2 = -1.00-1.41i``

Time Complexity to find roots of quadratic equation in C
O(1) will be the time complexity as general mathematics is used to find the roots of the quadratic equation.

Conclusion
This article provides the best explanation for solving quadratic equations in C. Solving problems like finding quadratic equation roots on a regular basis will help your programming career. Don’t stop there; try more logical questions like finding the roots of a quadratic equation, which will not only improve your logic but will also improve your programming skills.

Q1. How do I find the roots of a number in C?
Ans. In C programming, the sqrt() function is a pre-defined library function used to calculate the square root of a number.

Q2. What are the 4 ways to find the roots of a quadratic equation?
Ans. There are various methods by which you can solve a quadratic equation, such as: factorization, completing the square, the quadratic formula, and graphing. These are the four general methods by which we can solve a quadratic equation.

Q3. What is an algorithm to solve a quadratic equation?
Ans. Quadratic equations are the polynomial equations of degree 2 in one variable of type: f(x) = ax^2 +bx + c, where a, b, c, ∈ R and a ≠ 0. The general form of the quadratic equation is called the leading coefficient, and c is called the absolute term of f(x).

Q4. What is an example of a quadratic equation with real roots?
Ans. For the equation x^2-7x+12=0, on solving it, we have the real roots as 3 and 4.

Q5. What is C in quadratic standard form?
Ans. In the standard form of a quadratic function, f(x) = ax2 + bx + c, c is equal to the y-intercept of the graph of the function.