当前位置:网站首页>2.5 find the sum of the first n terms of the square root sequence

2.5 find the sum of the first n terms of the square root sequence

2022-06-25 19:57:00 Then peace and happiness

This question requires the preparation of procedures , Calculate the sequence of square roots √​1​​​+√​2​​​+√​3​​​+⋯ Before N Sum of items . Can contain header files math.h, And call sqrt The square root of a function .‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

Input format :

Input gives a positive integer on a line N.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

Output format :

In a row, press “sum = S” The format of the output part and the value of S, Accurate to two decimal places . The calculation results should not exceed the double precision range .‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

sample input :

10

sample output :

sum = 22.47

Reference code :

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

原网站

版权声明
本文为[Then peace and happiness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190512045971.html