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

Exercise 2-4 Find the sum of the first N items of the interleaved sequence (15 points)

This question requires a program to calculate the sum of the first N items of the interleaved sequence 1-2 / 3 + 3 / 5-4 / 7 + 5 / 9-6 / 11 + ...

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

Output format: The
partial sum value is output in one line, and the result retains three decimal places .

Sample input:

5

Sample output:

0.917

Code:

#include"stdio.h"
#include"math.h"
int main(){
    int N, i, m;
    double result;
    scanf("%d",&N);
    for(i = 1; i <= N; i++)
        {
            m = pow(-1, i+1);
            result += (1.0*i*m)/(2*i-1);
         }
         printf("%0.3lf", result);
    return 0;
}

I don't know what to say about this topic. I feel that I don't have any nibbling with you anymore (grieved blame).

Published 25 original articles · won 3 · views 240

Guess you like

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