What's Here?
- Members: 149,621
- Replies: 506,752
- Topics: 79,851
- Snippets: 2,666
- Tutorials: 706
- Total Online: 1,922
- Members: 66
- Guests: 1,856
|
This program computes all real and non-real (imaginary) roots/zeros for an equation using the quadratic formula.
|
Submitted By: SPlutard
|
|
Rating:

|
|
Views: 8,736 |
Language: C++
|
|
Last Modified: March 20, 2006 |
|
Instructions: This is a complete program, but feel free to eliminate/add to it in order to make it into a function for another program, or whatever is needed to fit your needs. It will work perfectly fine on its own, as well. |
Snippet
// imaginary_Quad_Form.cpp : This program will compute all the roots/zeros of a quadratic equation (including imaginary ones).
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
main()
{
float a, b, c, y1, y2, dis;
std::cout<<"This program will compute the roots/zeros of a quadratic equation (including imaginary ones).\n\n"<<"Please enter a, b, and c from the form: y = aX^2 + bX + c\n"<<"a = ";
std::cin>>a;
std::cout<<"b = ";
std::cin>>b;
std::cout<<"c = ";
std::cin>>c;
dis = ((b*b)-(4*a*c));
if(((b*b)-(4*a*c))<0)
{
dis = -1 * dis;
y1 = ((-1 * b) + (sqrt(dis)))/(2 * a);
y2 = ((-1 * b) - (sqrt(dis)))/(2 * a);
if(y1 == y2) std::cout<<"\a\nThe root is: "<<y1<<"i\n\n\n";
else std::cout<<"\a\nThe roots are: "<<y1<<"i and "<<y2<<"i\n\n\n";
}
else
{
y1 = ((-1 * b) + (sqrt(dis)))/(2 * a);
y2 = ((-1 * b) - (sqrt(dis)))/(2 * a);
if(y1 == y2) std::cout<<"\a\nThe root is: "<<y1<<"\n\n\n";
else std::cout<<"\a\nThe roots are: "<<y1<<" and "<<y2<<"\n\n\n";
}
system("Pause");
return 0;
}
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|