Home »
C++ programming language
div() Function with Example in C++
C++ div() function: Here, we are going to learn about the div() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 28, 2020
C++ div() function
div() function is a library function of cstdlib header. It is used for integral division, it accepts two parameters (numerator and denominator) and returns a structure that contains the quot (quotient) and rem (remainder).
Syntax
Syntax of div() function:
C++11:
div_t div (int numer, int denom);
ldiv_t div (long int numer, long int denom);
lldiv_t div (long long int numer, long long int denom);
Parameter(s)
- numer – represents the value of numerator.
- denom – represents the value of denominator.
Return value
The return type of this function is div_t / ldiv_t / lldiv_t, returns a structure that contains the quot (quotient) and rem (remainder).
Sample Input and Output
Input:
long int n = 10;
long int d = 2;
ldiv_t result;
Function call:
result = div(n, d);
Output:
result.quot = 5
result.rem = 0
Example
C++ code to demonstrate the example of div() function:
// C++ code to demonstrate the example of
// div() function
#include <iostream>
#include <cstdlib>
using namespace std;
// main() section
int main()
{
long int n = 10;
long int d = 2;
ldiv_t result;
result = div(n, d);
cout << "Numerator : " << n << endl;
cout << "Denominator: " << d << endl;
cout << "Quotient : " << result.quot << endl;
cout << "Remainder : " << result.rem << endl;
cout << endl;
n = 100;
d = 12;
result = div(n, d);
cout << "Numerator : " << n << endl;
cout << "Denominator: " << d << endl;
cout << "Quotient : " << result.quot << endl;
cout << "Remainder : " << result.rem << endl;
cout << endl;
n = 300;
d = 123;
result = div(n, d);
cout << "Numerator : " << n << endl;
cout << "Denominator: " << d << endl;
cout << "Quotient : " << result.quot << endl;
cout << "Remainder : " << result.rem << endl;
cout << endl;
return 0;
}
Output
Numerator : 10
Denominator: 2
Quotient : 5
Remainder : 0
Numerator : 100
Denominator: 12
Quotient : 8
Remainder : 4
Numerator : 300
Denominator: 123
Quotient : 2
Remainder : 54
Reference: C++ div() function