关闭

递归求阶乘和循环阶乘

857人阅读 评论(0) 收藏 举报
public class Factorial {
static long sum=0;
static long factorial=1;
public static void main(String [] args){
	java.util.Scanner in = new java.util.Scanner(System.in);
	int count = in.nextInt();
	for(int a=1; a<=count; a++){
	factorial=factorial*a;
	}
	System.out.println(factorial);
}
}
/*递归求阶乘*/
public class RecursionRequestFactorial {
public long factorial(int request_number){
	long value=0;
	if(request_number ==1 || request_number ==0){
		value =1;
	}else if(request_number > 1){
		value= request_number*factorial(request_number-1);
	}
	return value;
}
public static void main(String [] args){
	java.util.Scanner in = new java.util.Scanner(System.in);
	int request_number = in.nextInt();
	RecursionRequestFactorial start = new RecursionRequestFactorial();
	System.out.println(start.factorial(request_number));
}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14016次
    • 积分:268
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类