Wednesday, September 28, 2011

Program To Find the Roots Of Quadratic Equation




Quadratic Equations:
A quadratic equation is a polynomial equation of the second degree.The general form iswhere x represents a variable or an unknown, and a, b, and c are constants with a ≠ 0.The roots are given by the quadratic formula:
x=\frac{-b \pm \sqrt {b^2-4ac}}{2a},
Here is the coding to do it in C++ programming.

CODE: 
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c,x1,x2;
cout<<"Enter Values a,b,c for the Equation aX^2 + bX + c= 0 : ";
cin>>a>>b>>c;
float d=(b*b)-(4*a*c);
if(d==0)
{
x1=x2=(-b)/(2*a);
cout<<"Roots are Real and Equal x1=x2 :"<<x1;
}
else if(d>0)
{
x1=((-b)+sqrt(d))/(2*a);
x2=((-b)-sqrt(d))/(2*a);
cout<<"Roots of the equation are x1="<<x1<<"/n x2="<<x2;
}
else
{
cout<<"Roots are Imaginary";
}
getch();
}

Article by
Chief-Editor

He is a tech Enthusiast, an Altruist and calm mentor. He loves to code. He is also a creative graphic designer. Do check out his designfolio @ Jasmeet.asia If you like This post, you can follow me on Twitter.



For Further Reading,
C++ Program, Control Stuctures, If-Else-If Ladder Statement

0 comments:

Post a Comment