Write your question here.
hi ,I wrote this code and I don't knew what is wrong
in the output the result is always ZERO ...
Is it the library ??
I could use some help =(
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
usingnamespace std;
int main ()
{ int x, result;
cout<<"Evaluate: 1/(x^2+x+1)\n";
cout<<"ENTER THE VALUE OF X :";
cin>>x;
result=1/(x*x+x+1) ;
cout<<"1/(x^2+x+1) = "<<result;
cout<<endl;
return 0;
}
That is because of integer division, an int cannot hold a fractional part, so it truncates it. Change the type of your variables to double.
Also, it's always a good idea to check for division by zero. With double, check that the number is not less than some precision value like 0.00001 say. One cannot do a direct comparison with zero for doubles, have a read on Google as to why that is.
Even though the function may be strictly positive, it's worth doing a check anyway, it might save you one day if the math function is changed to something else that might produce 0.