Click here to Skip to main content
Click here to Skip to main content

A simple program to solve quadratic equations with

By , 11 Nov 2010
 
A simple program to calculate quadratic equation with.
 

Input MUST have the format:
AX2 + BX + C = 0
 

EXAMPLE: input the equation
2X2 + 4X -30 = 0 as:
 

A= 2 B= 4 C= -30
 

The answers for AX2 + BX + C = 0 should be 3 and -5.
 
x1=3
 
x2=-5
 

 
 
bool solver(float a,float b, float c, float &x1, float &x2)
{
	float delta = sqrt( (b*b) - (4*a*c) );
 
    if (!a)  return false; 
 
    x1 = ( -b + delta)/(2*a);
    x2 = ( -b - delta)/(2*a);
 
	return true;
}
 

int main()
{
	float a=2,
              b=4,
              c=-30; 
	
 
/*
 	printf("a = ");
	scanf("%f", &a);
	printf("b = ");
	scanf("%f", &b);
	printf("c = ");
	scanf("%f", &c);
 
*/
 
float x1,x2;
if ( solver(a, b ,c, x1, x2) ) 	printf("x1=%f\nx2=%f\n",x1,x2);
 
return 0;
} 
 
...

License

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

About the Author

Clark Kent SuperCoder
Sweden Sweden
Member
About me:
I attended programming School and I have a degree in three programming languages.
C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.
 
I've written about a dozen small simple applications and games.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 1 A major problem is it doesn't preven...memberAndrew Phillips7 Nov '10 - 15:01 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 11 Nov 2010
Article Copyright 2010 by Clark Kent SuperCoder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid