C Program To Find Roots of Quadratic Equation
Learn How To Find Roots of Quadratic Equation in C Programming Language using If – Else Block Structure. This Code To Calculate Quadratic Roots of an Equation is without Functions and also has an Output Screen displayed at the bottom of this page.
What is a Quadratic Equation?
The Standard Form of a Quadratic Equation is ax2 + bx + c = 0, where x is a variable entity and a, b, c are constant values which cannot be changed.
Formula For Quadratic Equation
z = −b ± √(b2 − 4ac) / 2a
Conditions For Discriminants
- If b2 − 4ac is Negative, Two Complex Solutions are possible
- If b2 − 4ac is Positive, Two Real Solutions are possible
- If b2 − 4ac = 0, One Real Solution is possible
Also Read: C Program To Remove Vowels From A String
C Program To Find Roots of Quadratic Equation
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 | #include<stdio.h> #include<math.h> int main() { float root1, root2, discriminant; float a, b, c; printf("\nEnter The Values\n"); printf("\nA:\t"); scanf("%f", &a); printf("\nB:\t"); scanf("%f", &b); printf("\nC:\t"); scanf("%f", &c); discriminant = b*b - 4*a*c; if(discriminant < 0) { printf("\nRoots Are Imaginary\n"); } else { root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a); printf("\nRoot 1 = %f\n", root1); printf("\nRoot 2 = %f\n", root2); } printf("\n"); return 0; } |
Note: If you Compile this Code in Linux, you will get a Linker Error. It is because the implementation of sqrt() method is missing and it is defined in libm library in GNU GCC Compiler. Therefore, you have to explicitly apply it while compilation.
Command: gcc test.c -lm
Also Read: Find Sum of Lower Triangular Elements of Matrix C Program
Output

If you have any compilation errors or doubts in this Program to Find Roots of Quadratic Equation in C Programming Language, let us know about in the Comment Section below.
It is really a short and simple code for finding quadratic roots in c language. Thanks.