Program to Print the Roots of a Quadratic equation.
first we find the delta as delta=b*b-4*a*c; as done in program , then three condition are checked like:
1 Real & Unequal
2 Real & Equal
3 Complex & Imaginary
(note- use of " math.h "header file for the solution of the square root of delta "sqrt(delta) " )
Source code:-
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main()
{clrscr();
cout<<"Program to Print Roots of Quadrtic Equation By Tarun Rawat \n";
float a,b,c,root1,root2,delta;
cout<<"Enter three constant a,b & c of ax^2 + bx + c \n";
cin>>a>>b>>c;
if(a==0)
cout<<"value of a not be zero ";
else{ delta=b*b-4*a*c;
if (delta>0)
{ root1= (-b+sqrt(delta))/(2*a);
root2= (-b-sqrt(delta))/(2*a);
cout<<"Roots are Real and Unequal\n";
cout<<"Root1= "<<root1<<"\nRoot2= "<<root2;
}
else{ if(delta==0)
{ root1=-b/(2*a);
cout<<"Roots are Real & Equal\n";
cout<<"Root1= "<<root1<<"\nRoot2= "<<root2;
}
else cout<<"Roots are Complex and Imaginary \n";
}
getch();
}
No comments:
Post a Comment