12,506,733 members (61,912 online)
Alternative Tip/Trick
alternative version

25.7K views
3 bookmarked
Posted

# 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

## You may also be interested in...

 Pro Pro

 First Prev Next
 You have missed one VISWESWARAN199814-Jun-16 20:18 VISWESWARAN1998 14-Jun-16 20:18
 Reason for my vote of 2 Can crash too ! YvesDaoust8-Nov-10 23:27 YvesDaoust 8-Nov-10 23:27
 thats the most basic rule in the book JFergulbops8-Nov-10 22:38 JFergulbops 8-Nov-10 22:38
 1) It doesn't matter what the real world quadratic equations... Silic0re098-Nov-10 4:34 Silic0re09 8-Nov-10 4:34
 Thank you for the suggestion.. I do not wish to be rude. If... pranav958-Nov-10 0:13 pranav95 8-Nov-10 0:13
 Reason for my vote of 2 Inefficient and does not handle divi... Andrew Phillips7-Nov-10 15:19 Andrew Phillips 7-Nov-10 15:19
 Reason for my vote of 2 Inefficient and does not handle divide by zero.
 This is better than the others as you avoid the domain error... Andrew Phillips7-Nov-10 15:14 Andrew Phillips 7-Nov-10 15:14
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Sep-16 23:14 Refresh 1