เพื่อตรวจสอบว่าจำนวนตัวหารเป็นคู่หรือคี่ รหัส 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' จะถูกเรียกด้วยค่าที่กำหนดไว้ ข้อความที่เกี่ยวข้องจะแสดงบนคอนโซล