This question requires writing a program to calculate the square root sequence
1 + 2 + 3 +… \sqrt{1}+\sqrt{2}+\sqrt{3}+...1+2+3+
TheN
sum of the antecedents of. You can include a header filemath.h
and call asqrt
function to find the square root.
Input format:
Enter a positive integer in one line N
.
Output format:
In one line, sum = S
output 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:
Problem-solving ideas:
No difficulty, you can refer to the ideas of the previous topics!