Experiment 4-2-2 Find the approximate value of e (15 points)

Natural constants ecan 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+1entry 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:

Insert picture description here

Problem-solving ideas:

For this kind of return value, it is doublerecommended to use doubletype variables inside the function , otherwise there will be unexplainable errors!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114624336