C div() function
C div() function - Calculate quotient and remainder
The div() function is used to calculate the quotient and remainder of the division of numerator by denominator.
Syntax div() function
div_t div(int numer, int denom)
Parameters div() function
Name | Description | Required /Optional |
---|---|---|
numer | The numerator. | Required |
denom | The denominator. | Required |
Return value from div() function
- A structure of type div_t, containing both the quotient int quot and the remainder integer rem.
- If the return value cannot be represented, its value is undefined.
- If denominator is 0, an exception will be raised.
Example: div() function
The following example shows the usage of div() function.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int nums[4] = {6,56,-34,-45};
int den[4] = {5,7,3,-7};
div_t ans; /* div_t is a struct type contain two ints:
'quot' stores quotient; 'rem' stores remainder*/
short i,j;
printf("Results of division:\n");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
ans = div(nums[i],den[j]);
printf("Dividend: %6d Divisor: %6d", nums[i], den[j]);
printf(" Quotient: %6d Remainder: %6d\n", ans.quot, ans.rem);
}
}
Output:
Results of division: Dividend: 6 Divisor: 5 Quotient: 1 Remainder: 1 Dividend: 6 Divisor: 7 Quotient: 0 Remainder: 6 Dividend: 6 Divisor: 3 Quotient: 2 Remainder: 0 Dividend: 6 Divisor: -7 Quotient: 0 Remainder: 6 Dividend: 56 Divisor: 5 Quotient: 11 Remainder: 1 Dividend: 56 Divisor: 7 Quotient: 8 Remainder: 0 Dividend: 56 Divisor: 3 Quotient: 18 Remainder: 2 Dividend: 56 Divisor: -7 Quotient: -8 Remainder: 0 Dividend: -34 Divisor: 5 Quotient: -6 Remainder: -4 Dividend: -34 Divisor: 7 Quotient: -4 Remainder: -6 Dividend: -34 Divisor: 3 Quotient: -11 Remainder: -1 Dividend: -34 Divisor: -7 Quotient: 4 Remainder: -6 Dividend: -45 Divisor: 5 Quotient: -9 Remainder: 0 Dividend: -45 Divisor: 7 Quotient: -6 Remainder: -3 Dividend: -45 Divisor: 3 Quotient: -15 Remainder: 0 Dividend: -45 Divisor: -7 Quotient: 6 Remainder: -3
C Programming Code Editor:
Previous C Programming: C abs()
Next C Programming: C labs()
C Programming: Tips of the Day
What's the point of const pointers?
const is a tool which you should use in pursuit of a very important C++ concept:
Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.
Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:
void foo(int* ptr) { ptr = 0;// oops, I meant *ptr = 0 }
If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.
Ref : https://bit.ly/33Cdn3Q
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises