Experiment 2-3-2 Find the sum of the first N items of the N-th sequence (15 points)

This question requires a program to calculate the sum of the first N items of the sequence 1 + 1/2 + 1/3 + ...

Input format:

The input gives a positive integer N on one line.

Output format:

The partial sum value S is output in the format of "sum = S" in one line, accurate to 6 decimal places. The problem is to ensure that the calculation result does not exceed the double precision range.

Sample input:

6

#include<stdio.h>
int main ()
{
    int n,i;
    double sum=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        sum+=1.0/i;
    }
    printf("sum = %.6lf",sum);
    return 0;
}
 

Sample output:

sum = 2.450000

Guess you like

Origin www.cnblogs.com/wven/p/12681569.html