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