forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
div.c
37 lines (31 loc) · 1.02 KB
/
div.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*******************************************************************************
*
* Program: div function demo
*
* Description: Example of us the div function in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=HwIFm7rKxCk
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num = 10;
int den = 3;
// we could calculate the quotient and remainder seperately with the division
// and modulus operations
// int quot = num / den;
// int rem = num % den;
// or we can use div to calculate both at once
div_t result = div(num, den);
// div returns a struct of type div_t with members quot and rem for the
// quotient and remainder values
printf("quot: %d\n", result.quot);
printf("rem: %d\n", result.rem);
// variations of the div function exist for long and other integer values:
// https://en.cppreference.com/w/c/numeric/math/div
return 0;
}