Draw a Flowchart & write an Algorithm to find Roots of Quadratic equations ax²+bx+c=0
Answers
Answer:
Step Form Algorithm:
- Start.
- Declare the required variables.
- Indicate the user to enter the coefficients of the quadratic equation by displaying suitable sentences using printf() function.
- Wait using the scanf() function for the user to enter the input.
- Calculate the roots of quadratic equation using the proper formulae.
- Display the result.
- Wait for user to press a key using getch() function.
- Stop.
Pseudo Code Algorithm:
- Start.
- Input a, b, c.
- D ← sqrt (b × b – 4 × a × c).
- X1 ← (-b + d) / (2 × a).
- X2 ← (-b - d) / (2 × a).
- Print x1, x2.
- Stop.
Flowchart:
Flowchart to calculate the roots of quadratic equation is shown below in figure 3.
Answer:
Quadratic Equation
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}