Calculate the factorial and sum of n using C language (multiple methods)
Calculate the factorial of a certain number:
# Include <stdio. h> int main () {int n, I; scanf ("% d", & n); for (I = n-1; I> 0; I --) {n = n * I;} printf ("% d", n); return 0 ;}
Calculate 1! + 2! + 3! +... + 10!
# Include <stdio. h> int main () {int a, B, c; int sum = 0; for (a = 1; a <= 10; a ++) // Control 1-10 digits {for (B = 1, c = 1; B <= a; B ++) // control the factorial of each number {c = B * c;} sum + = c; // accumulate the result in sum} printf ("% d", sum ); return 0 ;}
Calculate the sum of 1-10 factorial using a loop
# Include <stdio. h> # include <stdlib. h> int main () {int num = 1; int I = 0; int sum = 0; for (I = 1; I <= 10; I ++) {int tmp = I; // define a temporary variable to store I (because the variables in the for loop cannot be modified at Will) num = num * tmp; sum + = num ;} printf ("% d \ n", sum); return 0;