Click here to Skip to main content
13,247,891 members (44,220 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

28.8K views
3 bookmarked
Posted 10 Jun 2010

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)

Share

About the Author

Anshul R
Student
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThank you for the suggestion.. I do not wish to be rude. If... Pin
pranav958-Nov-10 1:13
memberpranav958-Nov-10 1:13 
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.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by Anshul R
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid