生活随笔
收集整理的這篇文章主要介紹了
求阶乘和
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
求階乘和。
Input
輸入小于10的正整數n。
Output
輸出s得值, s=1!+2!+…+n!
Sample Input
1
3
5
Sample Output
1
9
153
#include<stdio.h>
int fact(int n
);\\本題只求
10以內只需
int,數量大則需要
double
int main()
{int i
,n
,s
=0;\\遞加用s
=0,遞乘用s
=1scanf("%d",&n
);for(i
=1;i
<=n
;i
++)s
=s
+fact(i
);printf("%d",s
);return 0;
}
int fact(int n
)\\自定義函數不加;,但要加
{}
{int i
,result
=1;\\疊乘定義為
1for(i
=1;i
<=n
;i
++\\再次循環result
=result
*i
;return result
;\\前面用什么返回是寫什么
}
總結
以上是生活随笔為你收集整理的求阶乘和的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。