how to write an algorithm to solve a quadratic equation ax^2+bx+c=0
Answers
Answer:
Program to find all the roots of a quadratic equation
C
1 #include <stdio.h>
2 #include <math.h>
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