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ả:
Leave a Reply
You must be logged in to post a comment.