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

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: