0

How to declare sqrt ? As code is not running and telling error in sqrt

#include <iostream> using namespace std; int main() { int a , b , c , discriminant , x1 , x2 , real_part , imaginary_part ; cout << "Enter coefficients of a , b , c " << endl ; cin >> a >> b >> c ; discriminant = (b*b) - (4*a*c) ; if (discriminant>0) { cout << "Roots are real and different" ; x1 = (-b + sqrt(discriminant)) / (2*a) ; x2 = (-b - sqrt(discriminant )) /(2*a) ; cout << "x1 = " << x1 << "x2 = " << x2 ; } else if (discriminant == 0) { cout << "Roots are equal\n" ; x1 = (-b/2*a) ; cout << "x1=x2 = " << x1 ; } else { real_part = (-b/2*a) ; imaginary_part = sqrt (-discriminant) /2*a ; cout << "x1 is " << real_part <<"+" << imaginary_part << "i" ; } return 0; }

18th Nov 2022, 5:03 AM
Keshav Karn
Keshav Karn - avatar
3 Antworten
0
Include the math library: #include <cmath>
18th Nov 2022, 5:29 AM
Brian
Brian - avatar
0
Thanks Brian Sir
18th Nov 2022, 5:40 AM
Keshav Karn
Keshav Karn - avatar
0
Keshav Karn you're welcome!
18th Nov 2022, 5:47 AM
Brian
Brian - avatar