1.

How to write an algorithm to solve a quadratic equation ax^2+bx+c=0

Answer» PROGRAM to find all the roots of a quadratic equation C  1 #include 2 #include  4 int main() 5 { 6 DOUBLE a, b, c, discriminant, root1, root2, realPart, imaginaryPart; 7  8 printf("Enter coefficients a, b and c: "); 9 scanf(“%lf %lf %lf”,&a, &b, &c); 10 ​ 11 discriminant = b*b-4*a*c; 12 ​ 13 // CONDITION for real and different roots 14 if (discriminant > 0) 15 { 16 // sqrt() function returns square root 17 root1 = (-b+sqrt(discriminant))/(2*a); 18 root2 = (-b-sqrt(discriminant))/(2*a); 19 ​ 20 printf("root1 = %.2lf and root2 = %.2lf",root1 , root2); 21 } 22 ​ 23 //condition for real and equal roots 24 else if (discriminant == 0) 25 { 26 root1 = root2 = -b/(2*a); 27 ​ Output Input- Enter coefficients a, b and c: 1 2 3 Output- root1 =-1+1.41421 and root2 =-1+1.41421


Discussion

No Comment Found