Experiment 2-3-7 Find the partial sum of the square and reciprocal sequence (15 points)

This question requires m和n(m≤n)writing a program for two positive integers to calculate the sequence sum
m 2 + 1 / m + (m + 1) ​ 2 + 1 / (m + 1) +… + n 2 + 1 / nm^{2}+1 /m+(m+1)^{​2}+1/(m+1)+...+n^{2}+1/nm2+1/m+(m+1)2+1/(m+1)++n2+1/n

Input format:

Enter two positive integers on one line, m和n(m≤n)separated by spaces.

Output format:

In one line, sum = Soutput the partial sum value in the format of " " S, accurate to six digits after the decimal point. The title guarantees that the calculation result does not exceed the double precision range.

Input sample:

5 10

Sample output:

sum = 355.845635

Code:

# include <stdio.h>
# include <stdlib.h>

int main() {
    
    
    int m,n,i;
    double sum = 0.0;
    scanf("%d %d",&m,&n);
    for (i=m;i<=n;i++) {
    
    
        sum += (i * i);
        sum += (1.0 / i);
    }
    printf("sum = %.6lf",sum);
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

There is nothing to say about this question, just pay attention to the traversal range of the loop

Guess you like

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