-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Quadratic_Equation.c
65 lines (58 loc) · 2.13 KB
/
Quadratic_Equation.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem: Solving Quadratic equations in C.
Aim: A program that prints roots and also tells us the types of the roots of a quadratic equation ax²+bx+c=0
*/
#include<stdio.h>
#include<math.h>
void quadratic_equation_roots(){
// value a, b, and c;
double a, b, c;
double discriminant;
printf("Enter the co-efficients (x^2), x, and 1 of the quadratic equation (a*x*x+b*x+c)=0: ");
scanf("%lf%lf%lf",&a,&b,&c);
// calculate the discriminant (b^2 - 4ac)
discriminant=(b*b-4*a*c);
// If a is 0, then equation is not quadratic, but linear
if(a==0){
printf("\nThe equation is a linear equation, not a qudratic equation");
}
// discriminant is greater than 0 --> real and distinct roots
else if(discriminant>0){
printf("\nThe roots are unequal and both have real value. And the roots are,");
printf("\nroot1 = %.2lf",(-b+sqrt(discriminant))/(2*a));
printf("\nroot2 = %.2lf",(-b-sqrt(discriminant))/(2*a));
}
// discriminant is equal to 0 --> real and equal roots
else if(discriminant==0){
printf("\nThe roots are equal and both have real values. And the roots are, ");
printf("\nroot1 = root2 = %.2lf",-b/(2*a));
}
// discriminant is less than zero, imaginary roots
else{
printf("\nThe roots are complex numbers and have imaginary roots. And the roots are, ");
printf("\nroot1 = %.2lf + %.2lfi",-b/(2*a),sqrt(-discriminant)/(2*a));
printf("\nroot2 = %.2lf - %.2lfi",-b/(2*a),sqrt(-discriminant)/(2*a));
}
}
void main(){
quadratic_equation_roots();
}
/*
Sample Test Cases:
....Test case 1:
Enter the co-efficients (x^2), x, and 1 of the quadratic equation (a*x*x+b*x+c)=0: 3 -6 2
The roots are unequal and both have real value. And the roots are,
root1 = 1.58
root2 = 0.42
....Test case 2:
Enter the co-efficients (x^2), x, and 1 of the quadratic equation (a*x*x+b*x+c)=0: 6 -12 6
The roots are equal and both have real values. And the roots are,
root1 = root2 = 1.00
....Test case 3:
Enter the co-efficients (x^2), x, and 1 of the quadratic equation (a*x*x+b*x+c)=0: 4.5 6 9
The roots are complex numbers and have imaginary roots. And the roots are,
root1 = -0.67 + 1.25i
root2 = -0.67 - 1.25i
#Time Complexity: O(1)
#Space Complexity: O(1)
*/