Java 程式檢查除數的個數是偶數還是奇數


要檢查除數的個數是偶數還是奇數,Java 程式碼如下所示:

示例

 現場演示

import java.io.*;
import java.math.*;
public class Demo{
   static void divisor_count(int n_val){
      int root_val = (int)(Math.sqrt(n_val));
      if (root_val * root_val == n_val){
         System.out.println("The number of divisors is an odd number");
      }else{
         System.out.println("The number of divisors is an even number");
      }
   }
   public static void main(String args[]) throws IOException{
      divisor_count(25);
   }
}

輸出

The number of divisors is an odd number

Demo 類包含一個名為 divisor_count 的函式,用於檢查特定數除數的個數是偶數還是奇數。在主函式中,使用定義的值呼叫 divisor_count。相關訊息顯示在控制檯上。

更新日期:08-Jul-2020

139 次瀏覽

啟動你的 職業

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.