Zhejiang University Edition "C Language Programming (3rd Edition)" Exercises 2-3

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

This problem requires two positive integers m and n (m≤n) programming, and calculates the sequence m 2 +. 1 / m + (. 1 + m) 2 +. 1 / (m +. 1) + ⋯ + n 2 + 1 / n.

Input format:
Enter two positive integers m and n (m≤n) in one line, separated by spaces
.
Output format: The
partial sum value S is output according to the format of "sum = S" in one line, accurate to six decimal places . The problem is to ensure that the calculation result does not exceed the double precision range .

Sample input:

5 10

Sample output:

sum = 355.845635

Code:

#include"stdio.h"
int main()
{
    int m, n, i;
    double sum = 0;
    scanf("%d %d", &m, &n);
    if(m <= n)
    {
        for(i = m; i <= n; i++)
        {
            sum += i*i + 1.0/i;
        }
        printf("sum = %.6lf", sum);
    }
    return 0;
}
Published 25 original articles · won 3 · views 240

Guess you like

Origin blog.csdn.net/oxygen_ls/article/details/105393251