ในการหาผลหารและเศษเหลือใน Java โค้ดจะเป็นดังนี้ −
ตัวอย่าง
public class Demo{
public static void main(String[] args){
int my_dividend = 11, my_divisor = 7;
int my_quotient = my_dividend / my_divisor;
int my_remainder = my_dividend % my_divisor;
System.out.println("The value of computed quotient is = " + my_quotient);
System.out.println("The value of computed remainder is = " + my_remainder);
}
} ผลลัพธ์
The value of computed quotient is = 1 The value of computed remainder is = 4
คลาสที่ชื่อว่า Demo มีฟังก์ชันหลัก ซึ่งกำหนดค่าตัวหารและตัวหาร ผลหารและเศษที่เหลือหาได้โดยใช้ตัวดำเนินการ '/' และตัวดำเนินการโมดูลัส '%' ตามลำดับ ค่าถูกกำหนดให้กับตัวแปรต่างๆ และผลลัพธ์จะแสดงบนคอนโซล