Write a C++ program to find roots of a quadratic equation using Ternary conditional
operator.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a, b, c;
cout << "Please, enter a, b, c factors for a quadric equation: ";
cin >> a >> b >> c;
double discr = b*b - 4 * a*c;
if (discr > 0)
{
double root1 = (-b + sqrt(discr)) / (2 * a);
double root2 = (-b - sqrt(discr)) / (2 * a);
cout << "Quadric equation has real and different roots:\n"
<< "root1= " << root1 << "\n"
<< "root2= " << root2;
}
else if (discr == 0)
{
double root = -b / (2 * a);
cout << "Quadric equation has real and equal roots:\n"
<< "root1==root2=" << root;
}
else
{
cout << "Quadric equation has complex and different roots:\n";
double real = -b / (2 * a);
double imag = sqrt(-discr) / (2 * a);
cout << "root1= " << real << "+" << imag << "j\n";
cout << "root2= " << real << "-" << imag << "j\n";
}
}
Comments
Leave a comment