Exercise 4-3 Find the partial sum of a simple interleaved sequence with a given accuracy (15 points)

Exercise 4-3 Find the sum of the simple interleaved sequence part of the given precision (15 points)
This question requires writing a program to calculate the sequence part of the sum 1-1/4 + 1/7-1/10 +… until the absolute value of the last item is not Greater than the given precision eps.

Input format:
Input a positive real number eps in one line.

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

Input example 1:
4E-2
Output example 1:
sum = 0.854457
Input example 2:
0.02
Output example 2:
sum = 0.826310

#include<stdio.h>
int main()
{
    
    
    int i=1;
    double eps,sum=0.0;
    scanf("%lf",&eps);
    while(1.0/(3*i-2)>eps)
        {
    
    
            if(i%2==1)
                sum+=1.0/(3*i-2);
            else
                sum-=1.0/(3*i-2);
            i++;
        }
    if(i%2==1)
        sum+=1.0/(3*i-2);
    else
        sum-=1.0/(3*i-2);
    printf("sum = %.6lf",sum);
}

Guess you like

Origin blog.csdn.net/ChaoYue_miku/article/details/114857948