Given a numerator and denominator, we have to find their quotient and remainder without using the modulo or division operator. div() function allows us to do the same task easily and efficiently.**div() function : **Returns the integral quotient and remainder of the division of numer by denom ( numer/denom ) as a structure of type div_t, ldiv_t or lldiv_t, which has two members: quot and rem. **Syntax: **

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

When we use div() function, it returns a structure which contains the quotient and remainder of the parameters. The first parameter passed in a div() function is taken as numerator and the 2nd parameter is taken as denominator.

For int values, the structure returned is div_t. This structure looks like this:

## C++

`typedef` `struct` ` ` `{` ` ` `int` `quot; ` `/* Quotient. */` ` ` `int` `rem; ` `/* Remainder. */` `} ` `div_t` `;` |

Similarly, for long values, structure ldiv_t is returned and for long long values, structure lldiv_t is returned.

## C++

`ldiv_t` `:` `struct` `ldiv_t` `{` ` ` `long` `quot;` ` ` `long` `rem;` `};` `lldiv_t:` `struct` `lldiv_t {` ` ` `long` `long` `quot;` ` ` `long` `long` `rem;` `};` |

**Where is it useful ?**

The question is, since we have both % and / operators, why should we use div() function?. Well, in a program where we require both – quotient and remainder, using div() function would be the best choice as it calculates both the values for you at once, moreover, it requires less time as compared to using % and / functions one by one.

While using div() function, *both %operator and using div() will return the same value of remainder, i.e if we’re getting negative value of remainder by using %operator then we will get negative value of remainder using div() function too. Eg, div(-40,3) will give remainder of ‘-1’. *. So, div() function can be efficiently used according to one’s requirement. **What happens when the denominator is 0?**

If any one of the part of this function, i.e. the remainder or the quotient cannot be represented or cannot find a result, then the whole structure shows an **undefined behaviour** . **NOTE :** While using div() function, remember to include cstdlib.h library in your program.

Examples:

Input : div(40, 5) Output :quot = 8 rem = 0 Input :div(53, 8) Output :quot = 6 rem = 5 Input : div(-40,3) Output : quot = -13 , rem = -1

**Implementation:**

## CPP

`// CPP program to illustrate` `// div() function` `#include <iostream>` `#include <cstdlib>` `using` `namespace` `std;` `int` `main()` `{` ` ` `div_t` `result1 = ` `div` `(100, 6);` ` ` `cout << ` `"Quotient of 100/6 = "` `<<` ` ` `result1.quot << endl;` ` ` `cout << ` `"Remainder of 100/6 = "` `<<` ` ` `result1.rem << endl;` ` ` `ldiv_t` `result2 = ` `div` `(19237012L,251L);` ` ` `cout << ` `"Quotient of 19237012L/251L = "` `<<` ` ` `result2.quot << endl;` ` ` `cout << ` `"Remainder of 19237012L/251L = "` `<<` ` ` `result2.rem << endl;` ` ` `return` `0;` `}` |

**Output: **

Quotient of 100/6 = 16 Remainder of 100/6 = 4 Quotient of 19237012L/251L = 76641 Remainder of 19237012L/251L = 121

This article is contributed by **Ayush Saxena**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

**C++ Foundation Course**for Basic to Advanced C++ and

**C++ STL Course**for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer

**Complete Interview Preparation Course**

**.**