13,250,883 members (48,764 online)
Alternative Tip/Trick
alternative version

#### Stats

28.8K views
3 bookmarked
Posted 10 Jun 2010

# A simple program to solve quadratic equations with

, 8 Nov 2010
 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...

 View All Threads First Prev Next
 1) It doesn't matter what the real world quadratic equations... Silic0re098-Nov-10 5:34 Silic0re09 8-Nov-10 5:34
 1) It doesn't matter what the real world quadratic equations say, if I run your program, I have the freedom to (stupidly or not), put a zero where asked for "a". The program should recognize this and ask for a different number, not create a divide by zero exception. One great axiom of programming is to ALWAYS ASSUME your input data is dirty, incorrect, and malicious. You need to protect your program from the user.
 Last Visit: 31-Dec-99 19:00     Last Update: 20-Nov-17 3:52 Refresh 1