Respuesta :
Answer:
- int main(void)
- {
- float a, b, c, discriminant, root1, root2;
- printf("Enter value for a, b and c:");
- scanf("%f %f %f", &a, &b, &c);
- discriminant = b * b - 4 * a * c;
- if(discriminant < 0){
- printf("The roots are imaginary");
- }else{
- root1 = (-b + squareRoot(discriminant)) / (2*a);
- root2 = (-b - squareRoot(discriminant)) / (2*a);
- printf("Root 1: %f", root1);
- printf("Root 2: %f", root2);
- }
- return 0;
- }
Explanation:
Firstly, we declare all the required variables (Line 3) and then get user input for a , b and c (Line 4-5).
Next, apply formula to calculate discriminant (Line 7).
We can then proceed to determine if discriminant smaller than 0 (Line 9). If so, display the message to show the roots are imaginary (Line 10).
If not, proceed to calculate the root 1 and root 2 (Line 12-13) and then display the roots (Line 15 -16)
Answer:
// Program to calculate the roots of a quadratic equation
// This program is written in C programming language
// Comments are used for explanatory purpose
// Program starts here
#include<stdio.h>
#include<math.h>
int main()
{
// For a quadratic equation, ax² + bx + c = 0, the roots are x1 and x2
// where d =(b² - 4ac) ≥ 0
// Variable declaration
float a,b,c,d,x1,x2;
// Accept inputs
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
// Calculate d
d = (b*b) - (4*a*c);
// Check if d > 0 or not
if(d < 0){ // Imaginary roots exist
printf("The roots are imaginary");
}
else // Real roots exist
{
// Calculate x1 and x2
x1= ( -b + sqrt(d)) / (2* a);
x2 = ( -b - sqrt(d)) / (2* a);
// Print roots
printf("Roots of quadratic equation are: %.3f , %.3f",x1,x2);
}
return 0;
}
return 0;
}