Java程序使用递归查找给定数字的阶乘
递归是以自相似的方式重复项目的过程。在编程语言中,如果程序允许您在同一函数内调用函数,则称为函数的递归调用。
以下是使用递归函数查找给定数字的阶乘的示例。
import java.util.Scanner;
public class ab21_FactorialUsingRecursion {
public static long factorial(int i) {
if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to which you need to find the factorial");
int i = sc.nextInt();
System.out.println("Factorial of the given number is ::"+ factorial(i));
}
}输出结果
Enter the number to which you need to find the factorial 12 Factorial of the given number is ::479001600
热门推荐
7 破五的祝福语简短
8 建队节祝福语简短
10 奶茶店新年祝福语简短
11 破五祝福语简短的
12 生日祝福语 简短独特潮
13 新年祝福语简短又好听
14 朋友出国旅游祝福语简短
15 日生日贺卡祝福语简短
16 幼园开学简短祝福语
17 宝贝生日祝福语简短独特
18 朋友新工作祝福语简短