ddng.net
当前位置:首页 >> 用FACt函数求阶乘之和 >>

用FACt函数求阶乘之和

#include <stdio.h>int fac(int n);int fac(int n){ if(n<=1) return 1; else return n*fac(n-1);} main(){ int i,sum=0; for(i=1;i<=10;i++) { sum=sum+fac(i); } printf("%d\n",sum);}

Option ExplicitDim s As IntegerPrivate Sub Command1_Click() Text2.Text = Str(fact(Val(Text1.Text))) s = 0End SubPrivate Function fact(n As Integer) As Integer Dim i As Integer, j As Integer j = 1 fact = 0 For i = 1 To n j = j * i s = s + j Next i fact = sEnd Function

function fact(byval n as integer) as longdim i as integerdim s as longs = 1for i = 1 to n s = s * inextfact = send function

#include <stdio.h>int fact(int a);void main(){int m,i;long sum;printf("Input m:");scanf("%d",&m);sum=fact(m); for(i=1;i<=m;i++)printf("%d!%c",i,i<m?'+':'=');printf("%ld\n",sum);}int fact(int m){int i,f=1,sum=0;for(i=1;i<=m;i++){f=f*i;sum=sum+f;}return sum;}

1.scanf("%d,&n);少了半个引号2.关键错误,主函数main中sum应赋初值0

看看这套程序吧:#include<stdio.h> int fact(int n) { int ans=1,i; if(n<=1) return 1; for(i=1;i<=n; ++i) ans*=i; return ans; } int main() { int n,ans; scanf("%d",&n); ans=fact(n); printf("ans = %d\n",ans); return 0; }

#include<stdio.h> void main() { int i,t=1,sum=0; for(i=1;i<=10;i++) { t=t*i; sum=sum+t;if(i!=10)printf("%d!+",i);else printf("%d!=",i);}printf("%d",sum);}输出结果1!+2!+3!+4!+5!+6!+7!+8!+9!+10!=它们之和 引用来自: http://wenwen.soso.com/z/q341200018.htm?ri=2180&rq=341628533&uid=12509598&pid=w.xg.yjj&ch=w.xg.lldjj

#include <stdio.h>#include<stdlib.h> long long Fact(int n){ if(n==1)return 1; return n*Fact(n-1); } void main(){ int n; long long x=0; scanf("%d",&n); while(n>0)x+=Fact(n--); printf("\nx=%d\n",x); }

假如N = 10;#include#define N 10//阶乘 int fact(n) { if(n==0 || n == 1) { return 1; } else { return n*fact(n-1); } } int main() { int sum = 0; for(int i=1;i { sum += fact(i); } printf("sum = %d",sum); }

#include using namespace std; int Fact(int n) { int sum(1),i(1); while(i { sum*=i++; } return sum; } int main() { int n; cout cin>>n; int total(0); for(int i=1;i { total+=Fact(i); } cout system("pause"); return 0; } 希望对你有帮助

网站首页 | 网站地图
All rights reserved Powered by www.ddng.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com