Wednesday, December 3, 2014

Bài 18

Bài 18: Tính S(x,n) = 1 + x^2/2! + x^4/4! + ... + x^2n/(2n)!

//========================================
//Cách 1

#include <stdio.h>
#include <conio.h>

void main()
{
    float x;
    scanf("%f", &x);
    int n;
    scanf("%d", &n);
    float S = 1;
    float T = 1;
    long M = 1;
    int i = 2;
    while (i <= 2*n)
    {
        T = T * x * x;
        M = M * (i-1)*i;
        S = S + (float)T/M;
        i = i + 2;
    }
    printf("%f", S);
}

//========================================
//Cách 2
/*
#include <stdio.h>
#include <conio.h>

float Tong(float,int);
void main()
{
    float x;
    scanf("%f", &x);
    int n;
    scanf("%d", &n);
    float kq = Tong(x,n);
    printf("%f", kq);
}

float Tong(float y, int k)
{
    float S = 1;
    float T = 1;
    long M = 1;
    for (int i=2; i<=2*k; i=i+2)
    {
        T = T * y * y;
        M = M * (i-1)*i;
        S = S + (float)T/M;
    }
    return S;
}
*/

No comments:

Post a Comment