計算1/1! + 2/2! + 3/3! +...

Input Format 輸入一整數n Output Format 輸出計算1/1! + 2/2! + 3/3! + .... + n/n!的結果,取至小數點第五位
 avatar
user_6817964
c_cpp
3 years ago
249 B
3
Indexable
#include <stdio.h> // printf
int main()
{
	int n;
	scanf_s("%d", &n);
	float total = 0;
	for (float i = 1; i <= n; i++) {
		float mul = 1;
		for(int j = 1; j <= i; j++)
		    mul *= j;
		total += (i / mul);
	}
	printf("%6.5f", total);
}
Editor is loading...