
#include
Int main ()
{
Int sign;
Float sum, n;
Sign=1;
Sum=0.0;
For (n=1; N<=10000; N++)
{sum=sum + sign * (1/n);
Sign=1 * sign;
}
Printf (" % f ", sum);
}
So why not put n as int
CodePudding user response:
1/n is 01.0/n floating-point results can be attained
CodePudding user response:
1/n is two integer division, the result of integer division is an integer, decimal point total abandonment1.0/n is floating-point and integer division, will be converted into two floating-point division, that is all you need
CodePudding user response:
sum=sum + sign * (1.0/n);
Try this.
Need to keep 1/n is a floating-point division, or 1/n is integer division.