wnlt.net
当前位置:首页 >> 1!+2!+3!+……+n!求和用C语言编写,要求使用函数嵌套调用 >>

1!+2!+3!+……+n!求和用C语言编写,要求使用函数嵌套调用

函数嵌套调用:#include <stdio.h> long func(int n) { if(n == 1|| n == 0) return 1; else return n * func(n-1); } int main() { int i,n; long sum; scanf("%d",&n); sum = 0; for(i=1;i<=n;i++) sum += func(i); printf("sum = %ld\n",sum); return 0; }

#include<iostream> using namespace std; int main() { int n; long int s=0,t=1; cout<<"请输入n:"<<endl; cin>>n; for(int i=1;i<=n;i++) {t=t*i; s=s+t;} cout<<"1!+2!+3!+4!+……+n!="<<s<<endl; return 0; } 扩展资料:return表示把程序流程从被调函

#include <stdio.h>void main(){ int i,n,j; long int sum = 0; scanf("%d",&n); for( j = 0; j <n; ++j) { int s = 1; for(i=1;i<=j + 1;i++) { s*=i; } printf("%d!=%d\n",i - 1,s); sum += s; } printf("sum=%d\n",sum); getchar(); }

#include void main() { int i,j,n; long sum,m; sum=0; do{ printf("输入n:"); scanf("%d",&n); }while(n for(i=1;i { m=1; for(j=1;j m*=j; sum+=m; } printf("%ld\n",sum); }

#include <stdio.h> int fact(int y) { if(y < 2){ return 1; }else{ return y * fact(y - 1); } } int sum(int x) { if(x < 2){ return 1; }else{ return fact(x) + sum(x - 1); } } void main(void) { while(1){ int n; printf("Enter n:"); scanf("%d",&n); printf("sum=%d",sum(n)); } } 你的程序不叫递归(函数嵌套调用方法)

#include<stdio.h> long factor(int n) { long result=0; long temp=1; int i; for(i=1;i<=n;i++) { temp*=i; result+=temp; } return result; } int main() { int n; printf("please input a number\n"); scanf("%d",&n); long result=factor(n); printf("the result is %d\n"

利用for循环嵌套,就这样写:#include<stdio.h>void main(){ int n,i,j,t,s=0; printf("Input n:"); scanf("%d",&n); for(i=1; i<=n; i++) { t=1; for (j=1; j<=i; j++) { t*=j; } s+=t; } printf("sum:%d\n",s);}

c程序 #include void main() { int i; float n=1,sum=0; for(i=1;i void main() { int i; float n=1,sum=0; for(i=1;i

//---------------------------------------------------------------------------#include <stdio.h> long int s(int n) { if (n) return n*n+(s(n-1); else return 0; } int main() { int n; scanf("%d",&n); printf("%ld",s(n)); return 0; }//---------------------------------------------------------------------------

#include<stdio.h> int fun(int n) { int sum=0, i; for(i=1; i<=n; ++i) sum = sum + i; return sum; } void main() { int n, res; scanf("%d", &n); res = fun(n); printf("结果为:%d", res); }

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