Write a C++ program to find the roots of and quadratic equation of type a(x^2)+b(x)+c where a is not equal to zero.
Program Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x,y,z;
float root1, root2;
cout<<"Enter values of x : ";
cin>>x;
cout<<endl;
cout<<"Enter values of y : ";
cin>>y;
cout<<endl;
cout<<"Enter values of z : ";
cin>>z;
cout<<endl;
int dis = y*y-4*x*z;
if(dis == 0)
{
root1= (-y)/(2*x);
root2 = root1;
cout<<"Roots of quadratic equations are real & equal";
cout<<endl;
}
else if(dis > 0)
{
root1 = -(y + sqrt(dis))/(2*x);
root2=-(y - sqrt(dis))/(2*x);
cout<<"Roots of quadratic equations are real & distinct";
cout<<endl;
}
else
{
root1 = (-y)/(2*x);
root2 = sqrt(-dis)/(2*x);
cout<<"Roots of quadratic equations are imaginary";
cout<<endl;
}
cout<<" First root of quadratic equations is : "<<root1;
cout<<endl;
cout<<" Second root of quadratic equations is : "<<root2;
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter values of x : 1
Enter values of y : 4
Enter values of z : 4
Roots of quadratic equations are real & equal
First root of quadratic equations is : -2
Second root of quadratic equations is : -2
Thanks
Mukesh Rajput
Post A Comment:
0 comments: