-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5_1b.c
43 lines (43 loc) · 1.4 KB
/
lab5_1b.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary, discriminant;
printf("Enter value of 'a' of quadratic equation (aX^2 + bX + c): ");
scanf("%f", &a);
printf("Enter value of 'b' of quadratic equation (aX^2 + bX + c): ");
scanf("%f",&b);
printf("Enter values of 'c' of quadratic equation (aX^2 + bX + c): ");
scanf("%f",&c);
// Find discriminant of the equation
discriminant = (b * b) - (4 * a * c);
switch(discriminant > 0)
{
case 1:
// If discriminant is positive
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct and real roots exists: %.2f and %.2f",
root1, root2);
break;
case 0:
// If discriminant is not positive
switch(discriminant < 0)
{
case 1:
// If discriminant is negative
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
break;
case 0:
// If discriminant is zero
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
break;
}
}
return 0;
}