Write a C++ program to find roots of a quadratic equation using Ternary conditional operator.
#include <iostream>
#include <math.h>
using namespace std;
void roots(int x, int y, int z)
{
if (z == 0) {
cout << "Wrong Quadratic Equation\n";
return;
}
int det = y * y - 4 * x * z;
double sqrtVal = sqrt(abs(det));
if (det > 0) {
cout << (double)(-y + sqrtVal) / (2 * x) << "\n"<< (double)(-y- sqrtVal) / (2 * x);
}
else if (det == 0) {
cout << -(double)y / (2 * x);
}
else
{
cout << -(double)y / (2 * x) << " + i" << sqrtVal<< "\n"<< -(double)y / (2 * x) << " - i" << sqrtVal;
}
}
int main()
{
int x= 1, y= -7, z = 12;
roots(x, y, z);
return 0;
}
Comments
Leave a comment