Factorial
作者:
綿綿_ | 来源:发表于
2019-04-01 16:41 被阅读0次
使用循环计算阶乘
import java.util.Scanner;
public class Factorial {
private static Scanner input;
static long fact(int n)
{
int i;
long result=1;
for(i=1;i<=n;i++)
{
result*=i;
}
return result;
}
public static void main(String[] args)
{
int i;
System.out.println("enter a number");
input = new Scanner(System.in);
i=input.nextInt();
System.out.println("the factorial of "+i+" is "+fact(i));
}
}
使用递归计算阶乘
static long fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}
本文标题:Factorial
本文链接:https://www.haomeiwen.com/subject/etovbqtx.html
网友评论