Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java หาผลหารและเศษ (Quotient and Remainder)

ตัวอย่างโค้ด

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 มีฟังก์ชัน main กำหนดค่า dividend (ตัวถูกหาร) และ divisor (ตัวหาร) ใช้ตัวดำเนินการ / หาผลหาร (quotient) และตัวดำเนินการ modulus % หาเศษ (remainder) กำหนดให้ตัวแปรต่างๆ และแสดงผล