From 917bf2164175f9436f0796cf433bd00fe42f8a5e Mon Sep 17 00:00:00 2001 From: Justsvetoslavov Date: Sun, 17 Oct 2021 13:59:42 +0300 Subject: pract. 02 - Solutions --- pract_SI/pract. 02/task5.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pract_SI/pract. 02/task5.cpp (limited to 'pract_SI/pract. 02/task5.cpp') diff --git a/pract_SI/pract. 02/task5.cpp b/pract_SI/pract. 02/task5.cpp new file mode 100644 index 0000000..419d6d4 --- /dev/null +++ b/pract_SI/pract. 02/task5.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; +int main() +{ + double a, b, c; + double root1, root2, imaginary; + double discriminant; + + cout << "Finding the root of the equation a * x^2 + b * x + c = 0\n"; + + cout << "Enter the value of a: "; + cin >> a; + + if (a < 0.00000001) + { + cout << "If a is 0, then this is not a quadratic equation!\n"; + return 1; + } + + cout << "Enter the value of b: "; + cin >> b; + + cout << "Enter the value of c: "; + cin >> c; + + discriminant = (b * b) - (4 * a * c); + + if (discriminant > 0) + { + root1 = (-b + sqrt(discriminant)) / (2 * a); + root2 = (-b - sqrt(discriminant)) / (2 * a); + + cout << "Two distinct and real roots exist: " << root1 << " and " << root2 << endl; + } + else if (discriminant == 0) + { + root1 = root2 = -b / (2 * a); + + cout << "Two equal and real roots exist: " << root1 << " and " << root2 << endl; + } + else if (discriminant < 0) + { + root1 = root2 = -b / (2 * a); + imaginary = sqrt(-discriminant) / (2 * a); + + cout << "Two distinct complex roots exist: " << root1 << "+" << imaginary << "i and " << root2 << "-" << imaginary << "i\n"; + } + + return 0; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d