ฟังก์ชันไลบรารี C/C++ div_t div(int numer, int denom) ทำการหาร numer (ตัวเศษ) ด้วย denom (ตัวส่วน) ดังนี้คือการประกาศฟังก์ชัน div()
div_t div(int numer, int denom)
พารามิเตอร์คือตัวเศษและตัวส่วน ฟังก์ชันนี้คืนค่าในโครงสร้างที่กำหนดใน <cstdlib> ซึ่งมีสมาชิก 2 ตัวสำหรับ div_t: int quot; int rem;
ตัวอย่างโค้ด
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
div_t output;
output = div(27, 4);
cout << "Quotient part of (27/ 4) = " << output.quot << endl;
cout << "Remainder part of (27/4) = " << output.rem << endl;
output = div(27, 3);
cout << "Quotient part of (27/ 3) = " << output.quot << endl;
cout << "Remainder part of (27/3) = " << output.rem << endl;
return 0;
}
ผลลัพธ์
Quotient part of (27/ 4) = 6 Remainder part of (27/4) = 3 Quotient part of (27/ 3) = 9 Remainder part of (27/3) = 0