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

計算1 + 1/2 + 1/3 +...+ 1/N,取至小數點第二位
 avatar
user_6817964
c_cpp
2 years ago
175 B
1
Indexable
Never
#include <stdio.h> // printf
int main()
{
	int n;
	scanf_s("%d", &n);
	float sum = 0;
	for (float i = 1; i <= n; i++) {
		sum += (1 / i);
	}
	printf("%3.2f", sum);
}