Bài 11: Tính S(n) = 1 + 1x2 + 1x2x3 + ... + 1x2x3xn
//========================================//Cách 1
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
scanf("%d", &n);
long S = 0;
long T = 1;
int i = 1;
while (i <= n)
{
T = T * i;
S = S + T;
i = i + 1;
}
printf("%ld", S);
}
//========================================
//Cách 2
/*
#include <stdio.h>
#include <conio.h>
long Tong(int);
void main()
{
int n;
scanf("%d", &n);
long kq = Tong(n);
printf("%ld", kq);
}
long Tong(int k)
{
long S = 0;
long T = 1;
for (int i=1; i<=k; i++)
{
T = T * i;
S = S + T;
}
return S;
}
*/
No comments:
Post a Comment