BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan

BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan (http://bzupages.com/)
-   Object Oriented Programming (http://bzupages.com/322-object-oriented-programming/)
-   -   Program to find the roots of a quadratic equation (http://bzupages.com/f322/program-find-roots-quadratic-equation-14656/)

bonfire 08-03-2011 11:58 PM

Program to find the roots of a quadratic equation
 
1 Attachment(s)
Program to find the roots of a quadratic equation


#include
#include
#include
int main()
{
clrscr();
float a,b,c,d,root1,root2;
cout << "Enter the 3 coefficients a, b, c : " << endl;
cin>>a>>b>>c;
if(!a){
if(!b)
cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n";
else
{
d=-c/b;
cout << "The solution of the linear equation is : " << d << endl;
}
}
else
{
d=b*b-4*a*c;
if(d>0)
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout << "The first root = " << root1 << endl;
cout << "The second root = " << root2 << endl;
}
getch();
return 0;
}
This program takes in the values of the three coefficients a, b, and c as a screen input from the user.
It then determines the roots of the quadratic equation using the formula ax^2 + bx + c = 0.
The two roots are then outputted using the 'cout' command.

Sample Input 4 4 -3
Sample Output
The first root = 0.5
The second root = -1.5


All times are GMT +5. The time now is 10:28 PM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.