Answer to Question #262528 in C++ for Ali

Question #262528

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


1
Expert's answer
2021-11-07T17:18:24-0500


#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;
}

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