Roots of quadratic equation giving wrong answer

Below code is given wrong answer.

#include<iostream>
#include<cstdio>
#include<cmath>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);


using namespace std;

int main()
{
	FAST_IO

int A,B,C;

cin>>A>>B>>C;
double val=(B*B)-(4*A*C);
int  x1=(-B+sqrt(val))/(2*A);
int x2=((-B-sqrt(val))/(2*A));

cout<<x1<<"\n"<<x2<<"\n";


    return 0;
}


For what testcase?

my output should satisfy the below condition.
Your output will be considered to be correct if the difference between your output and the actual answer is not more than 10^−6.

Without seeing a link to the original problem, it’s hard to know for sure, but having the answers x1 and x2 be ints instead of doubles will only give the correct answer in fairly rare circumstances.

x1 and x2 should be doubles

Below is the link to the problem:

yeah i have tried with int type for x1 and x2 but i am still getting the wrong answer.
I am suspecting the sqrt fuction because it returns double.

its not working for double type for both x1 and x2

I think while printing x1 and x2 with cout doesn’t gave output till precision 10^-6 .
instead use setprecision or printf for printing with a precision of 10^-6.
eg. printf("%0.6lf\n%0.6lf",x1,x2);

1 Like

Thanks man