The Program in C that calculates the real roots of the quadratic equation is given below:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter the values of a, b and c: ");
scanf("%f %f %f", &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("The roots are real and different.\n");
printf("root1 = %.2f and root2 = %.2f", root1, root2);
}
//condition for real and same roots
else if (discriminant == 0) {
root1 = root2 = -b / (2*a);
printf("The roots are real and same.\n");
printf("root1 = root2 = %.2f;", root1);
}
//if roots are not real
else {
printf("The roots are complex and different.");
}
return 0;
}
Output:
Enter the values of a, b, and c: 23
45
99
The roots are complex and different.
Pro-Tips💡
This program prompts the user to enter the values of the coefficients (a, b, and c) of the quadratic equation (ax^2 + bx + c = 0).
Then it uses the formula to calculate the discriminant (b^2 – 4ac) to check if the roots are real or complex.
If the discriminant is greater than 0, the roots are real and different, if the discriminant is equal to 0 the roots are real and the same, and if the discriminant is less than 0, the roots are complex and different.
The program then uses the appropriate formula to calculate the roots and prints them out.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.