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

This question requires writing a program to calculate the square root sequence
​ 1 + 2 + 3 +… \sqrt{​1}+\sqrt{2}+\sqrt{3}+...1 +2 +3 +
TheNsum of the antecedents of. You can include a header filemath.hand call asqrtfunction to find the square root.

Input format:

Enter a positive integer in one line N.

Output format:

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

Input sample:

10

Sample output:

sum = 22.47

Code:

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

int main() {
    
    
    int N,i;
    scanf("%d",&N);
    double sum = 0.0;
    for (i=1;i<=N;i++) {
    
    
        sum += (sqrt(i));
    }
    printf("sum = %.2lf",sum);
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

No difficulty, you can refer to the ideas of the previous topics!

Guess you like

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