Answer :
Answer:
from math import sqrt
print("Solving equation ax^2 + bx + c \n")
a = int(input('Enter value of a: '))
b = int(input('Enter value of b: '))
c = int(input('Enter value of c: '))
determinant = b*b - 4*a*c;
if (a == 0 and b == 0) :
print("There are no solutions.")
elif(a == 0 and b != 0) :
print("There is just one root.")
root = c/b
print("Root: ", root)
elif (determinant < 0) :
print("There are no real roots")
x = sqrt(-determinant)/(2*a)
print("Root1: %d + %.2fi" % (-b, x))
print("Root2: %d + %.2fi" % (-b, -x))
else :
root1 = (-b + sqrt(determinant))/(2*a)
root2 = (-b - sqrt(determinant))/(2*a)
print("Root1:", root1)
print("Root2:", root2)
Explanation:
void findRoots(int a, int b, int c)
{ // If a is 0, then equation is not quadratic, but linear
if (a == 0)
{
double root = (-1)*c/b;
printf("only one root %f", root);
return;
}
int d = b*b - 4*a*c;
double sqrt_val = sqrt(abs(d));
if (d > 0)
{
printf("Roots are real and different \n");
printf("%f\n%f",(double)(-b + sqrt_val)/(2*a) , (double)(-b - sqrt_val)/(2*a));
}
else if (d == 0)
{
printf("Roots are real and same \n");
printf("%f",-(double)b / (2*a));
}
else // d < 0
{
printf("Roots are complex \n");
printf("%f + i%f\n%f - i%f", -(double)b / (2*a),sqrt_val ,-(double)b / (2*a), sqrt_val);
}
}
int main()
{
Int a, b, c;
printf("Enter an integer: ");
scanf("%d", &a);
printf("Enter an integer: ");
scanf("%d", &b);;
printf("Enter an integer: ");
scanf("%d", &c);
findRoots(a, b, c);
return 0;
}