Thương và dư là các phần của phép chia cùng với số bị chia và số chia.
- Số bị chia:
dividend - Số chia:
divisor - Kết quả phép chia:
quotient(thương) - Số còn lại:
remainder(dư)
Mối quan hệ: dividend = divisor * quotient + remainder
Ví dụ: 15 chia 7, thương 2, dư 1. Tại đây 15 là số bị chia, 7 là số chia.
15 = 7 * 2 + 1
Chương trình tìm thương và dư:
Ví dụ
#include <iostream>
using namespace std;
int main() {
int divisor, dividend, quotient, remainder;
dividend = 15;
divisor = 7;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "Dividend is " << dividend << endl;
cout << "Divisor is " << divisor << endl;
cout << "Quotient is " << quotient << endl;
cout << "Remainder is " << remainder;
return 0;
}Đầu ra
Dividend is 15 Divisor is 7 Quotient is 2 Remainder is 1
Thương lấy bằng phép chia /, dư lấy bằng toán tử modulus %.
quotient = dividend / divisor; remainder = dividend % divisor;