Header Ads Widget

C++ program to determine the roots of quadratic equation

C++ program to determine the roots of quadratic equation

The objective of the code is to find the roots of the quadratic equation . For calculating the roots of the quadratic equation we will use the formula . 

Sample input 
2 3 1
Sample output
Roots of the equation are: -0.5 and -1



 Code(C++):-

#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,D;
float x1,x2,m;
cout<<"Enter the coefficient of the each term of the quadratic equation\n";
cin>>a>>b>>c;
D=(b*b)-(4*a*c);
if(D<0)
cout<<"_Imaginary roots\n";
else
{
m=sqrt(D);
x1=(-b+m)/(2*a);
x2=(-b-m)/(2*a);
cout<<"Roots of the equation are: "<<x1<<" and "<<x2;
}
return 0;
}

Output:-


Enter the coefficient of the each term of the quadratic equation
2 3 1
Roots of the equation are: -0.5 -1.0


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-