# C Language Examination first question detailed process

Source: Internet
Author: User
Tags mul

1. Calculate and output its results.

The idea is to first design a function to calculate the factorial, and then use the loop, summing each.

`#include <stdio.h>intMulintN) {intnum,i; Num=1;  for(i=1; i<=n;i++) Num=num*i;return(num);} voidMain () {inti,sum=0;  for(i=1; i<=Ten; i++) Sum+=Mul (i); printf ("The sum is%d\n", sum); }`

This code can only calculate the 1~10 of the factorial and slightly modify it:

`#include <stdio.h>intMulintN) {    intNum,i; Num=1;  for(i=1; i<=n;i++) Num=num*i; return(num);}voidMain () {inti,n,sum=0; printf ("Enter the number:\n"); scanf ("%d",&N); if(n<=0) printf ("error\n"); Else    {         for(i=1; i<=n;i++) Sum+=Mul (i); printf ("The sum is%d\n", sum); }}`

Obviously a lot better, you can calculate the sum of 1~n factorial, but there is more than the risk of an integer, rather than directly modified to:

`#include <stdio.h>DoubleMulintN) {    inti; Doublenum; Num=1.0;  for(i=1; i<=n;i++) Num=num*i; return(num);}voidMain () {intI,n; Doublesum=0; printf ("Enter the number:\n"); scanf ("%d",&N); if(n<=0) printf ("error\n"); Else    {         for(i=1; i<=n;i++) Sum+=Mul (i); printf ("The sum is%d\n", sum); }}`

The result was a lot bigger.

C Language Examination first question detailed process

Related Keywords:

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.