Java 中數字階乘的除數


以下是一個 Java 程式,用於查詢一個數字的階乘的除數。

程式

import java.util.Scanner;

public class DivisorsOfFactorial {
   public static long fact(int i) {
      if(i <= 1) {
         return 1;
      }
      return i * fact(i - 1);
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the n value :");
      int n = sc.nextInt();
      int result = 0;
      long fact = fact(n);
     
      for (int i = 1; i<= fact; i++) {
         if (fact%i == 0) {
            result = result+i;
         }
      }
      System.out.println(result);
   }
}

輸出

Enter the n value :
4
60

更新於: 2020 年 6 月 25 日

169 次瀏覽

職業生涯起步

透過完成課程取得認證

開始
廣告
© . All rights reserved.