Finding roots of equations in C Language
#include <math.h>
#include <stdio.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a: ");
scanf("%f", &a);
printf("Enter coefficients b: ");
scanf("%f", &b);
printf("Enter coefficients c: ");
scanf("%f", &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 = %f and root2 = %f", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %f;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("roots are not real");
}
return 0;
}
Output:
Enter coefficients a: 2 Enter coefficients b: 5 Enter coefficients c: 3 root1 = -1.000000 and root2 = -1.500000c language tutorial learn c language study c language