Tính tổng S(n) = 1/(1*2) + 1/(2*3) + … +1/(n*(n+1))

Yêu cầu:
– Nhập vào số nguyên dương n
– Tính tổng S(n) = 1/(1*2) + 1/(2*3) + … +1/(n*(n+1))

Giải thuật:
– Dùng vòng lặp for, do while để duyệt biến i từ 1 tới n
– Tính tổng: s = s + 1/(i*(i + 1))

Code

/************************************************************
* Author: VNCODING
* History
* 2016/12/12        first create    VNCODING
*************************************************************/

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

int main()
{
	printf("S(n) = 1/(1*2) + 1/(2*3) + … +1/(n*(n+1))\n");
	int i, n;
    float s = 0.0;
	do
	{
		printf("n = ");
		scanf("%d", &n);
	}while(n <= 0);
	
	for(i = 1; i <= n; i++)
	{
        s += 1.0/(i*(i + 1));
	}
	printf("\nS(%d) = %f", n, s);
	getch();
	return 0;
}

Kết quả:

Tính tổng S(n) = 1/(1x2) + 1/(2x3) + … 1/(nx(n+1))
Tính tổng S(n) = 1/(1×2) + 1/(2×3) + … 1/(nx(n+1))

Be the first to comment

Leave a Reply