Write a function to calculate roots of a quadratic equation of the form ax^2+bx+c=0. The formula to find the roots of a Quadratic equation is given below.
x= -b+/- (b^2-4ac)^1/2/2a
Input Specification :
Input 1 : A double value representing the coefficient value of x^2 in a quadratic equation.
Input 2 : A double value representing the coefficient value of x in a quadratic equation.
x= -b+/- (b^2-4ac)^1/2/2a
Input Specification :
Input 1 : A double value representing the coefficient value of x^2 in a quadratic equation.
Input 2 : A double value representing the coefficient value of x in a quadratic equation.
Input 3 : A double value representing the coefficient value of 1 in a quadratic equation.
Output Specification :
Output : Return a double array representing the value of the two roots.
Note : Consider the output root values round off to 3 decimal places. Assume all roots are real.
Example 1
Input 1: 1
Input 2: -2
Input 3: -3
Output : { 3.0,-1.0}
Code :-
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, d;
double root1, root2;
scanf("%lf%lf%lf", &a, &b, &c);
d = b*b - 4*a*c;
if (d < 0)
{
printf("{%.2lf + i%.2lf,%.2lf - i%.2lf}", -b/(double)(2*a), sqrt(-d)/(2*a),-b/(double)(2*a), sqrt(-d)/(2*a));
}
else {
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("{%.1lf,%.1lf}", root1,root2);
}
return 0;
}
Output Specification :
Output : Return a double array representing the value of the two roots.
Note : Consider the output root values round off to 3 decimal places. Assume all roots are real.
Example 1
Input 1: 1
Input 2: -2
Input 3: -3
Output : { 3.0,-1.0}
Code :-
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, d;
double root1, root2;
scanf("%lf%lf%lf", &a, &b, &c);
d = b*b - 4*a*c;
if (d < 0)
{
printf("{%.2lf + i%.2lf,%.2lf - i%.2lf}", -b/(double)(2*a), sqrt(-d)/(2*a),-b/(double)(2*a), sqrt(-d)/(2*a));
}
else {
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("{%.1lf,%.1lf}", root1,root2);
}
return 0;
}