溫馨提示×

java怎么用遞歸實現(xiàn)n的階乘

小億
84
2024-03-01 11:44:14
欄目: 編程語言

public class Factorial {

    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static void main(String[] args) {
        int n = 5;
        int result = factorial(n);
        System.out.println(n + "! = " + result);
    }
}

0