div() - Utility Function

How is Utility function "div()" used in C++?
How to perform Integer Division in C++?

Explanation

div() is a Utility Function that returns the quotient and remainder in the "div_t" type for integers. For long integers the quotient and remainder is returned using the "ldiv_t" type defined by the header file.

Syntax to Integer Division:


div_t div(int numerator, int denominator);
ldiv_t div(long numerator, long denominator);

Example :



#include <stdlib.h>
#include <stdio.h>
int main ()
{
div_t r;
r=div(13,2);
printf ("The quotient of 13/2 is: %d\n", r.quot);
printf ("The remainder is:%d", r.rem);
return 0;
}

Result :

The quotient of 13/2 is: 6
The remainder is: 1

In the above example div() is used to perform integer division and find the quotient and the remainder of 13 is divided by 2.

C++ Tutorial


Ask Questions

Ask Question