sqrt function

#include <iostream>
#include<math.h>
using namespace std;

int main ()
{
int a, b, c, x;
cout <<"put values pliss master"<<endl;
cin>>a>>b>>c;
x=(-b+sqrt((b*b)-(4*a*c)))/2*a;
cout<<x<<endl;
return 0;
}
my sqrt function is giving an error idk why
Check that you are not trying to find the square root of a negative number.

Use double instead of int for the variables.

Divide by (2*a).
/2*a means divide by 2 then multiply by a.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    double a, b, c;
    cout <<"put values pliss master"<<endl;
    cin >> a >> b >> c;
    
    if (b*b - 4*a*c < 0)
    {
        cout << "No real roots\n";
    }
    else
    {
        double x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
        cout << x1 << endl;
    }
    
} 
Topic archived. No new replies allowed.