C Program to find roots of a quadratic equation using If Else statement
Program
#include<stdio.h>
#include<math.h>
void main()
{
int a, b, c;
float disc, root1, root2, real, imag;
printf("Enter the values of a, b and c\n");
scanf("%d %d %d", &a, &b, &c);
disc = ((b*b)-(4*a*c));
if(disc > 0)
{
root1 = (-b + sqrt(disc))/(2*a);
root2 = (-b - sqrt(disc))/(2*a);
printf("Roots are real and complex\n");
printf("Root 1:\t%f\n", root1);
printf("Root 2:\t%f\n", root2);
}
else if(disc == 0)
{
root1 = root2 = -b/(2*a);
printf("Roots are equal\n");
printf("Root:\t%f\n", root1);
}
else
{
real = -b/(2*a);
imag = sqrt(-disc)/(2*a);
printf("Roots are imaginary\n");
printf("Root 1:\t%f + i%f\n", real, imag);
printf("Root 2:\t%f - i%f\n", real, imag);
}
}
Output 1
Enter the values of a, b and c
1
2
1
Roots are equal
Root: -1.000000
Output 2
Enter the values of a, b and c
6
8
2
Roots are real and complex
Root 1: -0.333333
Root 2: -1.000000
Output 3
Enter the values of a, b and c
6
3
3
Roots are imaginary
Root 1: 0.000000 + i0.661438
Root 2: 0.000000 - i0.661438