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.2…n
Giải thuật:
– Dùng vòng lặp for, do while để duyệt biến i từ 1 tới n
– Khởi tạo: p = 1, s = 0
– Tính tích: p = p*i
– Tính tổng: s = s + p
Code
/************************************************************ * Author: VNCODING * History * 2016/12/14 first create VNCODING *************************************************************/ #include <stdio.h> #include <conio.h> int main() { printf("S(n) = 1 + 1.2 + 1.2.3 + ... + 1.2...n\n"); int i, n; unsigned int s = 0; unsigned int p = 1; do { printf("n = "); scanf("%d", &n); }while(n <= 0); for(i = 1; i <= n; i++) { p *= i; s += p; } printf("\nS(%d) = %d", n, s); getch(); return 0; }
Kết quả:
Leave a Reply
You must be logged in to post a comment.