snrg.net
当前位置:首页 >> n的阶乘C语言程序whilE >>

n的阶乘C语言程序whilE

12345678910 int main() { long long fact=1; int n; do{ fact*=n--; }while(n>0); printf("%lld\n",fact); }

#include<stdio.h> main() { int i,n,sum=1; scanf("%d",&n); for(i=1;i<=n;i++) //这里多了个分号.. sum*=i; //必须用另外一个变量来存储阶乘的结果.因为i是用来控制循环的. printf("%d",sum); getch(); }

整型范围内能表示阶乘 #include <stdio.h> int main() { int n,i,sum; sum=1; scanf("%d",&n); if(n<0) printf("shuruwuxiao!"); else{ for(i=1;i<=n;i++) sum=sum*i; printf("%d",sum); } return 0; }

#include <stdio.h> int main() { int j; printf("请输入一个整数:\n"); scanf("%d", &j); if (j <= 0) { return 0; } int i = 1,n = 1; for (i = 1; i <= j;++i) { n *= i; } printf("%d的阶乘等于:%d\n", j, n); return 0; }

你的while执行时变量i没有初始化(没有值),因此,while循环被架空 要解决这个问题 你在while语句之前,必须给i赋初始值(比如写上i=1;之类的)

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

用数组一位一位的乘

#include<stdio.h> int main() { int n,t,i; t=1; scanf("%d",&n); for(i=0;i<=n;i++) t=t*i; printf("%d\n",t); return 0; }

#include <stdio.h> int main() { int a=100; double num=1.0;//while 循环相乘实现阶乘 while(a>0) { num *= a; a--; } printf("100!的结果:%le\n",num);//for循环相乘实现阶乘 num=1; for(a=1;a<=100;a++) { num *= a;} printf("100!的结果:%le\n",num); return 0; }

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TTest{ class Sjx {static void Main(string[] args) {int n; Console.WriteLine("输入n的值"); n=Convert.ToInt32( Console.ReadLine()); int i = 1; while (n > 1) { i = i * n; n--; } Console.WriteLine("n的阶乘:"+i); Console.ReadKey(); } }}

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