Alternative Tip

# A simple program to solve quadratic equations with

, 8 Nov 2010
 Rate this:
Please Sign up or sign in to vote.
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);}```

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

Student
India
No Biography provided

## Comments and Discussions

 First Prev Next
 Reason for my vote of 2 Can crash too ! YvesDaoust 8-Nov-10 23:27
 thats the most basic rule in the book JFergulbops 8-Nov-10 22:38
 This is better than the others as you avoid the domain error in sqrt(). A couple of things: - pow(b, 2) is better written as b*b - it's probably better not to evaluate sqrt(d) twice - the original returned two floats, you should return an error instead of complex numbers
 Last Visit: 31-Dec-99 18:00     Last Update: 13-Jul-14 22:40 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by Anshul R
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid