This problem requires programming, calculates the sequence 1 + 1/2 + 1/3 + ...
and the first N entries.
Input format:
Enter a positive integer N in one line.
Output format:
In one line, sum = S
output the partial sum value in the format of " " S
, accurate to 6 digits after the decimal point. The title guarantees that the calculation result does not exceed the double precision range.
Input sample:
6
Sample output:
sum = 2.450000
Code:
# include <stdio.h>
# include <stdlib.h>
int main() {
int N,i;
double sum = 0.0;
scanf("%d",&N);
for (i=1;i<=N;i++) {
sum += (1.0 / i);
}
printf("sum = %.6lf",sum);
return 0;
}
Submit screenshot:
Problem-solving ideas:
There is no difficulty in this question, but you still need to pay attention 1.0 / i
to this. This has been explained before, and 1 / i
sometimes the result will be wrong if you use it directly !