Quadratic equations are the polynomial equation having a degree 2. For a quadratic equation ax2+ bx + c = 0
, (a≠0) and the discriminant (D = b2-4ac
) decides the nature of roots. There are three possibilities of discriminant and those are:
- when d>0, the roots are real and distinct
- when d=0, the roots are real and equal
- when d<0, the roots are real and imaginary
In order to understand the program, you need to have the knowledge of the following in C Programming:
Find the Roots of a Quardratic Equation in C
The program uses sqrt()
library function in order to calculate the square root of a number. To use sqrt()
, math.h
header file is used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <math.h> #include <stdio.h> int main() { double a, b, c, d, root1, root2, real, img; printf("Enter the values for a, b and c:\n"); scanf("%lf %lf %lf", &a, &b, &c); //determinant d = b *b - 4 *a * c; // real and different roots if (d > 0) { root1 = (-b + sqrt(d)) / (2 *a); root2 = (-b - sqrt(d)) / (2 *a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); } //for real and equal roots else if (d == 0) { root1 = root2 = -b / (2 *a); printf("root1 = root2 = %.2lf;", root1); } // d < 0 both roots are real and imaginary else { real = -b / (2 *a); img = sqrt(-d) / (2 *a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", real, img, real, img); } return 0; } |
Output: