11,410,118 members (62,096 online)
Alternative Tip/Trick

# A simple program to solve quadratic equations with

, 8 Nov 2010 CPOL
 Rate this:
Simple and prints imaginary roots too!float a,b,c,x1,x2,d,dsq;printf("ax^2 + bx + c = 0");printf("\nEnter a,b,c separated by commas : \n");scanf("%f,%f,%f",&a,&b,&c);d = b*b-(4*a*c);if(d>=0){dsq=sqrt(d);x1 = (-b+dsq)/(2*a);x2 = (-b-(dsq))/(2*a);printf("\nRoot 1 : %f\nRoot 2...
Simple and prints imaginary roots too!
```float a,b,c,x1,x2,d,dsq;
printf("ax^2 + bx + c = 0");
printf("\nEnter a,b,c separated by commas : \n");
scanf("%f,%f,%f",&a,&b,&c);
d = b*b-(4*a*c);
if(d>=0)
{
dsq=sqrt(d);
x1 = (-b+dsq)/(2*a);
x2 = (-b-(dsq))/(2*a);
printf("\nRoot 1 : %f\nRoot 2 : %f",x1,x2);
}
if(d<0)
{
d = ((4*a*c)-pow(b,2))/(2*a);
printf("\nRoot 1 : %f+%fi",((-b)/(2*a)),d);
printf("\nRoot 2 : %f-%fi",((-b)/(2*a)),d);}```

## Share

Student
India
No Biography provided

 First Prev Next
 Reason for my vote of 2 Can crash too ! YvesDaoust 9-Nov-10 0:27
 thats the most basic rule in the book JFergulbops 8-Nov-10 23:38
 Thank you for the suggestion.. I do not wish to be rude. If I am, I'm really sorry. 1) In a quadratic equation, 'a' can never be 0. So, where can a division by zero problem occur? 2) Taking square root twice is not a big issue.. It is computed in a matter of seconds.. I do agree it is inefficient in the extreme case.. 3) An alternative can have an improved feature.
 Last Visit: 31-Dec-99 19:00     Last Update: 25-Apr-15 6:24 Refresh 1