-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRootsOfQuadraticEq.c
43 lines (40 loc) · 1.29 KB
/
RootsOfQuadraticEq.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
/*Finding roots of Quadratic Equation*/
#include <stdio.h>
#include <math.h>
int main()
{
float i, a, b, c, D, root, root1, root2, real, img;
char choice;
do
{
printf("Enter the coefficient of a: ");
scanf("%f", &a);
printf("Enter the coefficient of b: ");
scanf("%f", &b);
printf("Enter the coefficient of c: ");
scanf("%f", &c);
D = (b * b) - 4 * a * c;
if (D == 0)
{
root = (-b) / (2 * a);
printf("Root is %f", root);
printf("\nRoots are real and equal\n");
}
else if (D > 0)
{
root1 = (-b + sqrt(D)) / (2 * a);
root2 = (-b - sqrt(D)) / (2 * a);
printf("Roots are %f and %f", root1, root2);
printf("\nRoots are real and different");
}
else
{
real = (-b) / (2 * a);
img = sqrt(-D) / (2 * a);
printf("Roots are %.2f+%.2fi AND %.2f-%.2fi", real, img, real, img);
printf("\nRoots are imaginary and different");
}
printf("\nWant to calculate roots one more time?(Press Y or y for YES / Press N or n for NO): ");
scanf(" %c",&choice);
} while(choice == 'Y' || choice == 'y');
}