Solving any quadratic equation by c++:
THEORY:
The general form of a quadratic equation is
ax2 + bx
+ c = 0 where a, b, c are real numbers (constants) and a ≠ 0, while b and c may
be zero.
Let D=b2 – 4ac= Discriminant
1. If D>0, roots are real & different
R1=-b+sqrt(b2
– 4ac) & R2=-b-sqrt(b2 – 4ac)
2.
If D=0, roots are real & equal
R1=R2=-b/2a
3.
If D<0, roots are complex &
different
R1=(-b/2a)+i(sqrt(b2
– 4ac)/2a)
R1=(-b/2a)-i(sqrt(b2
– 4ac)/2a)
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,x1,x2,realp,imgp,D;
clrscr();
cout<<"enter the coefficient
a,b,c="<<endl;
cin>>a>>b>>c;
D=(b*b)-(4*a*c);
if(D>0)
{
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);
cout<<"roots are real and
different"<<endl;
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
else if(D==0)
{
cout<<"the roots are real and same"<<endl;
x1=x2=(-b)/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
else
{
realp=(-b)/(2*a);
imgp=sqrt(-D)/(2*a);
cout<<"roots are complex and
different"<<endl;
cout<<"x1="<<realp<<"+"<<imgp<<"i"<<endl;
cout<<"x1="<<realp<<"-"<<imgp<<"i"<<endl;
}
getch();
}
OUTPUT:
Model syllabus (+3): Downoad
List of all practicals: Download
Get all Books: Download
- How to Prepare for IIT-JAM ⇠ ⇠ Click Here
- How to prepare for JEST ⇠ ⇠ Click Here
Post a Comment