Natural constants e
can use the series 1 + 1 1! + 1 2! +… + 1 n! +… 1+\frac{1}{1!}+\frac{1}{2!}+...+\frac{ 1}{n!}+...1+1!1+2!1+⋯+n!1+... to approximate calculations. This problem requires a given nonnegative integern
, the series before seekingn+1
entry and.
Input format:
Enter a non-negative integer given in the first line n(≤1000)
.
Output format:
Output the partial sum value in one line, keeping eight places after the decimal point.
Input sample:
10
Sample output:
2.71828180
Code:
# include <stdio.h>
# include <stdlib.h>
double fact(int n) {
double value = 1,i;
for (i=1;i<=n;i++) {
value *= i;
}
return value;
}
int main() {
int n,i = 0;
double value = 0.0;
scanf("%d",&n);
for (;i<=n;i++) {
value += (1.0/fact(i));
}
printf("%.8lf",value);
return 0;
}
Submit screenshot:
Problem-solving ideas:
For this kind of return value, it is double
recommended to use double
type variables inside the function , otherwise there will be unexplainable errors!