hhjc.net
当前位置:首页 >> 用whilE语句求阶乘 >>

用whilE语句求阶乘

m-- } printf("阶乘=%d")#include"请输入你要求的数字&quot,m); if(m} else while(m>stdio;=1){ sum=sum*m;1){ printf(");输入错误".h&quot,sum=1; printf(" sum); scanf("%d" void main(){ int m

int s=1;for(int i=1;is=s*i; } 或者 int i=1; int s=1; while(i{ s=s*i; } 求出来的s即n的阶乘

#include<stdio.h> int main() {int n,i=1; float f=1.0; printf("please as input an integer>=0.\n"); scanf("%d",&n); while(i<=n)f*=i++; printf("factorial of %d is:%.2f\n",n,f); system("pause"); return 0; }

#include int main() {int n,m,s=1; printf("请输入n="); scanf("%d",&n); m=n; while(n>=1) {s*=n; n--; } printf("%d的阶乘s=%d\n",m,s); return 0; }

#include<stdio.h> int main() { long int i=5,sum=1; while(i) { sum*=i; i--; } printf("%ld\n",sum); }

第一个while后面多了一个; ,把n=1放大片while之前, n++放到循环体的最后.这样修改后,两个程序效果就一样了,看起来代码没问题,如果是计算的从1一直加到20的阶乘的和,方法也正确,但感觉你的数据类型用错了, 20的阶乘肯定用int表示不了,会溢出,你换成long试试吧

#include <stdio.h> int main(void) { unsigned long sum,t,i; sum=0; t=1; i=1; while(i<11) { t*=i; sum+=t; i++; } printf("%lu\n",sum); return 0; }

Function myfunc(n As Integer) Dim i As Integer Dim sum As Long sum = 1 i = 1 While i sum = sum * i i = i + 1 Wend myfunc = sum End Function

#include "iostream.h" void main() { int a = 6; int b = 1; int c = 1; while(b != a) { c = b * c; ++b; } cout << c << endl; }

/*用while语句语句求1到20的阶乘的和*/ #include #define N 20 void main() { int i, sum, sum1; sum = 1; i = 2; while (i #define N 20 void main() { int i, sum, sum1; sum = 1; i = 2; do { sum1 = sum1 * i; sum = sum + sum1; i++; } while (i

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