C++ cstdlib div() Function
Example
Divide 17 by 5, and print the quotient and the remainder:
div_t result = div(17, 5);
cout << "Quotient: " << result.quot << "\n";
cout << "Remainder: " << result.rem;
Definition and Usage
The div() function divides one whole number by another. It returns both the quotient (the result of the division, without decimals) and the remainder (what is left over).
The result is a div_t structure with two members: quot (the quotient) and rem (the remainder).
The div function is defined in the <cstdlib> header file.
Tip: You get the same results with the arithmetic operators / and %: 17 / 5 is 3, and 17 % 5 is 2.
Syntax
div_t div(int numerator, int denominator);
There are also versions for long and long long numbers. They return an ldiv_t and an lldiv_t structure, with the same members.
Parameter Values
| Parameter | Description |
|---|---|
| numerator | Required. The number to divide. |
| denominator | Required. The number to divide by. It must not be 0. |
Technical Details
| Returns: | A div_t structure with two members: quot (the quotient) and rem (the remainder). |
|---|