Thứ Hai, 30 tháng 11, 2015

Bài 17: Tính S(n) = x + x^2/2! + x^3/3! + ... + x^n/N!

Bài 17: Tính S(n) = x + x^2/2! + x^3/3! + ... + x^n/N!a
#include <stdio.h> #include <conio.h> #include <math.h> int main() { int n = 3, x = 4; float S = 0; int i = 1; int gt = 1, lt; //lt = 1;   while(i <= n) { lt = (int)pow((float)x, i); //lt = lt * x   gt = gt * i; S = S + (float)lt / gt; i++; } printf("Ket qua: %2.2f", S); getch(); }


Chạy code: while -> printf
i = 1:
1 <= 3 chạy lt = x chạy gt = 1 chạy S = x / 1
i = 2:
2 <= 3 chạy lt = x^2 chạy gt = 1*2 chạy S = x / 1 + x^2 / (1 * 2)
i = 3:
3 <= 3 chạy lt = x^3 chạy gt = 1*2*3 chạy S = x / 1 + x^2 / (1 * 2) + x^3 / (1 * 2 * 3)

i = 4 <= 3 dừng thoát in S = x / 1 + x^2 / (1 * 2) + x^3 / (1 * 2 * 3) 004