Displaying wrong result . Why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Displaying wrong result . Why?

#include <iostream> #include <cmath> using namespace std; int main() { int a,b,c; cin >>a; cin >>b; // values // cin >>c; int d = sqrt((b*b)-(4*a*c)); // discriminant // int alpha = -b+d/2*a; //formula // int beeta = -b-d/2*a; cout <<"D="<<d<<endl; cout <<"alpha="<<alpha<<endl; cout <<"beeta="<<beeta<<endl; return 0; }

19th Mar 2022, 7:35 AM
Gagan Gajendra
4 Answers
+ 2
Becz you have written int alpha = -b+d/2a and same for beta Here you need to put round bracket u dividing value of d only It should be like this (-b +d)/2a ...
19th Mar 2022, 8:02 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Not too sure, but you might need to check whether the use of `float` or `double` as type for <a>, <b> and <c> helps. I'm just guessing it was due to integer division issue. (Edit) Might be considerable to rather define all variables involved in the calculation as floating point type.
19th Mar 2022, 8:40 AM
Ipang
+ 1
It is only working for few equations.
19th Mar 2022, 8:27 AM
Gagan Gajendra
+ 1
These lines are incorrect: int alpha = -b+d/2*a; /7/formula // int beeta = -b-d/2*a; It is an error in order of operations. Understand that -b+d/2*a is interpreted as -b+(d/2)*a. Change them to: int alpha = (-b+d)/(2*a); /7/formula // int beeta = (-b-d)/(2*a); And as already pointed out, it is likely a better choice to use floats instead of ints if accuracy is important.
19th Mar 2022, 10:40 AM
Brian
Brian - avatar