Answer to Question #264656 in C++ for saliha

Question #264656

Write a C++ program to find roots of a quadratic equation using Ternary conditional

operator.


1
Expert's answer
2021-11-11T17:33:55-0500
#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";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS