[C language programming] Write a program, input a positive integer i; calculate the value of the following series, keep 2 decimal places. Define and call the function double m (int i), implemented with a recursive method.

Not much to say, just go to the code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
    int n, i, x;
    double sum = 0;
    while (scanf_s("%d", &n) != -1) 
    {
        if (n > 0)
        {
            x = 1;
            for (i = 1; i <= 2 * n - 1; i += 2)
            {
                if (x % 2 != 0)
                {
                    sum += 1.0 / i;
                }
                else
                {
                    sum -= 1.0 / i;
                }
                x++;
            }
            printf("%.2lf\n", sum*4);
        }
    }
    return 0;

 Look more, knock more, understand more

 

 

Guess you like

Origin blog.csdn.net/TIG_JS/article/details/127677805