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

Exercise 2-5 Find the sum of the first N terms of the square root sequence (15 points)

This question requires a program to calculate the sum of the first N terms of the square root sequence √1 + √22 + √ 3 ++ ⋯. May include the header file math.h and call the sqrt function to find the square root.

Input format:
Enter a positive integer N in one line.

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

Sample input:

10

Sample output:

sum = 22.47

Do you feel that the topics are the same? Asi, it ’s not difficult, you just did it without a sense of accomplishment, hey! ! !
Code:

#include"stdio.h"
#include"math.h"
int main()
{
    int N, i;
    double sum = 0;
    scanf("%d", &N);
    for(i = 1;i <= N; i++)
    {
        sum += sqrt(i);
    }
    printf("sum = %0.2lf",sum);
    return 0;
}
Published 25 original articles · won 3 · views 240

Guess you like

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