Để tìm thương và phần dư trong Java, mã như sau:
Ví dụ
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); } }
Kết quả
The value of computed quotient is = 1 The value of computed remainder is = 4
Lớp Demo chứa hàm main, dividend và divisor định nghĩa. Thương và phần dư tìm bằng toán tử / và %. Giá trị gán cho biến khác và kết quả hiển thị.