0% found this document useful (0 votes)
22 views1 page

Untitled 1

This C++ program takes in coefficients a, b, and c and uses the quadratic formula to determine if the roots of a quadratic equation are real, real and the same, or complex based on the discriminant. It then outputs the appropriate root values.

Uploaded by

Marija Spasovska
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views1 page

Untitled 1

This C++ program takes in coefficients a, b, and c and uses the quadratic formula to determine if the roots of a quadratic equation are real, real and the same, or complex based on the discriminant. It then outputs the appropriate root values.

Uploaded by

Marija Spasovska
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <cmath>
using namespace std;

int main() {

float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;


cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" <<
endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" <<
endl;
}

return 0;

You might also like