Calculate the factorial and sum of n using C language (multiple methods)

Source: Internet
Author: User

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;

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.