Tính S(n) = 
//========================================
//Cach 1
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
printf("n = ");
scanf("%d", &n);
float S = 0;
long M = 0;
float dau = 1;
for (int i=1; i<=n; i++)
{
M = M + i;
S = S + dau*(float)1/M;
dau = -dau;
}
printf("S = %f", S);
}
//========================================
//Cach 2
#include <stdio.h>
#include <conio.h>
float Tong(int);
void main()
{
int n;
printf("n = ");
scanf("%d", &n);
float kq = Tong(n);
printf("S = %f", kq);
}
float Tong(int k)
{
float S = 0;
long M = 0;
float dau = 1;
for(int i=1; i<=k; i++)
{
M = M + i;
S = S + dau*(float)1/M;
dau = -dau;
}
return S;
}
No comments:
Post a Comment