Yêu cầu:
– Nhập vào số nguyên dương n
– Tính tổng S(x, n) = x + x^2 + x^3 +…+ x^n
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 + x^i
– Dùng hàm pow để tính x^i
Code
/************************************************************ * Author: VNCODING * History * 2016/12/14 first create VNCODING *************************************************************/ #include <stdio.h> #include <conio.h> #include <math.h> int main() { printf("S(x, n) = x + x^2 + x^3 +...+ x^n\n"); int i, n; float x; float s = 0.0; printf("x = "); scanf("%f", &x); do { printf("n = "); scanf("%d", &n); }while(n <= 0); for(i = 1; i <= n; i++) { s += pow(double(x), double(i)); } printf("\nS(%d) = %f", n, s); getch(); return 0; }
Kết quả:
Leave a Reply