php用whilE求n的阶乘

public class digui { public static void main(string[] args) { digui dg = new digui(); //求5的阶乘 int result = dg.getresult(5); } public int getresult(int n){ if(n==1){ return 1; }else{ return n*getresult(n-1); } } }

阶乘指从1乘以2乘以3乘以4一直乘到所要求的数.阶乘没有0,-1 都 是正整数

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; }

while<?php$p=1;$i=1; while($i<11) { $p*=$i; echo "$i"."!="."$p"; //输出阶乘结果 $i++; }?> dowhile的话,代码差不多,只要把 while($i<11)改成while($i<10)就可以了.本人也是PHP新手,呵呵,共勉.

#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(int argc, char **argv) { int n, i = 1, count=1; scanf (" %d ", &n); if(0 == n) // !0= 1 { printf("%d\n", 1); return 0; } while(i <= n) { count*=i; i++; } printf("%d\n",count); return 0; } PS:“用c语言的while编写n

#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(); }

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

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(); } }}

相关文档

php用递归求n的阶乘
递归法求n的阶乘
用递归实现5的阶乘
php实现函数n的阶乘和
php中实现100的阶乘
php语言计算阶乘n
用php实现1-n-的阶乘和
php编程计算1到n的阶乘
php编写计算n 的程序
php阶乘代码
php阶乘
利用递归法求5的阶乘
php计算n
php用while循环求阶乘
递归求5的阶乘
php求n的阶乘
利用递归方法求5
用php算出e的阶乘
电脑版