C Online Compiler
Example: C Program to Find Roots of Quadratic Equation using sqrt() Function
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C Program to Find Roots of Quadratic Equation using sqrt() Function #include <stdio.h> #include <math.h> // math.h header file loaded to call the sqrt() function int main() { double p, q, r, d, r1, r2, rp, ip; // p, q and r - To store the real numbers // d - To store the discriminant // r1 - To store the first root // r2 - To store the second root // rp - To store the real part // ip - To store the image part printf("-----Enter the these coefficients a, b and c----\n"); scanf("%lf %lf %lf", &p, &q, &r); d = q * q - 4 * p * r; if (d > 0) { // It is the condition for real and different roots r1 = (-q + sqrt(d)) / (2 * p); r2 = (-q - sqrt(d)) / (2 * p); printf("\nThe r1 = %.2lf & r2 = %.2lf\n", r1, r2); } else if (d == 0) { // It is the condition for real and equal roots r1 = r2 = -q / (2 * p); printf("\nThe r1 = r2 = %.2lf;\n", r1); } else { // if the roots are not real number rp = -q / (2 * p); ip = sqrt(-d) / (2 * p); printf("\nThe r1 = %.2lf+%.2lfi & r2 = %.2f-%.2fi\n", rp, ip, rp, ip); } return 0; }
4.2 4 6.5
Output
Clear
ADVERTISEMENTS