Easy Tutorial
❮ C Examples Write File C Function Toupper ❯

C Library Function - div()

C Standard Library - <stdlib.h>

Description

The C library function div_t div(int numer, int denom) divides numer (numerator) by denom (denominator).

Declaration

Following is the declaration for the div() function.

div_t div(int numer, int denom)

Parameters

Return Value

The function returns a value of type structure defined in <cstdlib>, which has two members: int quot; int rem;.

Example

The following example demonstrates the use of the div() function.

#include <stdio.h>
#include <stdlib.h>

int main()
{
   div_t output;

   output = div(27, 4);
   printf("Quotient of (27/ 4) = %d\n", output.quot);
   printf("Remainder of (27/4) = %d\n", output.rem);

   output = div(27, 3);
   printf("Quotient of (27/ 3) = %d\n", output.quot);
   printf("Remainder of (27/3) = %d\n", output.rem);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

Quotient of (27/ 4) = 6
Remainder of (27/4) = 3
Quotient of (27/ 3) = 9
Remainder of (27/3) = 0

C Standard Library - <stdlib.h>

❮ C Examples Write File C Function Toupper ❯