C++ Program To Find Whether The Solutions Of A Quadratic Equation Are Real Or Imaginary

C++ Program to Find All Roots of a Quadratic Equation

A quadratic equation is represented by: ax^2+bx+c=0(where a ≠ 0).
Here, a, b and c are the coefficients of x^2, x and x^o.
And x is the unknown variable (or the root of a quadratic equation ).

The roots of a quadratic function are given by,

x =( -b + or – (square root (b^2 -4ac))/2a

The term b^2−4ac is called discriminant which tells how many roots a quadratic function has. Specifically, if

  1. b^2 −4ac < 0 There are no real roots.
  2. b^2 −4ac = 0 There is one real root.
  3. b^2 −4ac > 0 There are two real roots.

Code:

#include <iostream>
#include<cmath>

using namespace std;

int main()
{
cout<<"a*x^2+b*x+c=0 is a quadratic equation"<<endl;
float x,y,i;
int a, b,c, d;
cout<<"enter the coefficent a, b and c"<<endl;
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d>0)
{
cout<<" The solution is real"<<endl;
x=(-b+sqrt(d))/(2*a);
y=(-b-sqrt(d))/(2*a);

cout<<"x=" <<x<< endl;
cout<<"y=" <<y<< endl;
}
else if (d==0)
{
cout<<"the solutions are real and equal"<<endl;
x=(-b+sqrt(d))/(2*a);
cout<<"x=y"<<x<<endl;

}
else
{
cout<<"the solution is imaginary"<<endl;
x=-b/(2*a)+sqrt(-d)/(2*a)*i;
y=-b/(2*a)+sqrt(d)/(2*a)*i;

cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
return 0;
}

Output:

C++ Program to Find All Roots of a Quadratic Equation
C++ Program To Find Whether The Solutions Of A Quadratic Equation Are Real Or Imaginary

Also Read: C++ Program To Calculate Area And Perimeter Of Rectangle

LEAVE A REPLY

Please enter your comment!
Please enter your name here