Experiment 2-3-2 Find the sum of the first N terms of the Nth sequence (15 points)

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 = Soutput 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:

Insert picture description here

Problem-solving ideas:

There is no difficulty in this question, but you still need to pay attention 1.0 / ito this. This has been explained before, and 1 / isometimes the result will be wrong if you use it directly !

Guess you like

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