- #include <stdio.h>
- #include <conio.h>
- void main()
- {
- int n = 3;
- float S = 0;
- int i = 1;
- while(i <= n)
- {
- S = S + (float)i / (i +1);
- i++;
- }
- printf("Tong la: %0.2f", S);
- getch();
- }
Chạy code: 9 to 14
i = 1:
1 <= 3 chạy S = S + i / (i + 1) = 0 + 1 / (1 + 1)
i = 2:
2 <= 3 chạy S = S + i / (i + 1) = 0 + 1 / (1 + 1) + 2 / (2 + 1)
i = 3:
3 <= 3 chạy S = S + i / (i + 1) = 0 + 1 / (1 + 1) + 2 / (2 + 1) + 3 / (3 + 1)
i = 4:
4 <= 3 dừng thoát khỏi vòng lặp
In ra S = 0 + 1 / (1 + 1) + 2 / (2 + 1) + 3 / (3 + 1) = 1.92
