Learning division in school means long division. The process involves a quotient and a remainder. For example, 42÷8 works out to 5 (quotient) with 2 remainder. On a computer, however, division renders the result as 5.25. So how do you get the remainder separated?
Before rolling headlong into dreaded math, consider a review of terms as presented in Figure 1.
With division, you have two values: The numerator or dividend, and the denominator or divisor. The numerator is on top. The denominator, like all demons, goes below. (That’s how I remember it.) The “gazinta” format for division is also shown in Figure 1 (item C). This is the form taught first in school, which is why the remainder becomes a thing.
In programming, the /
operator handles division. In my C programming books, I recommend using float or double values to obtain the accurate results. When you use integers, the result is truncated.
If you desire to discover the remainder, you can pull a few tricks. The first one is from a blog post on the div() function, which returns a structure containing the quotient and remainder of a division operation. When you just need to know the remainder, however, you can use the aptly-named remainder() function. Here’s its man page definition:
double remainder(double x, double y)
This function requires including the math.h
header file. In Linux, you must specify the -lm
switch when compiling to link in the math library.
The following code demonstrates the remainder() function.
2024_10_26-Lesson.c
#include <stdio.h> #include <math.h> int main() { double dividend,divisor,r; printf("Enter the dividend (numerator): "); scanf("%lf",÷nd); printf("Enter the divisor (denominator): "); scanf("%lf",&divisor); r = remainder(dividend,divisor); printf("%.2f/%.2f = %.2f with %.2f remainder\n", dividend, divisor, dividend/divisor, r ); return 0; }
The function prompts for two double values, which are then thrust into the remainder() function:
r = remainder(dividend,divisor);
A printf() statement outputs the results, which include standard division to obtain the precise value. But the value of variable r
is what’s left over — the remainder. Here’s a sample run:
Enter the dividend (numerator): 87.0
Enter the divisor (denominator): 6.0
87.00/6.00 = 14.50 with 3.00 remainder
This test run threw me:
Enter the dividend (numerator): 29.0
Enter the divisor (denominator): 3.0
29.00/3.00 = 9.67 with -1.00 remainder
The result of 29 ÷ 3 is 9 with 2 remaining. But the -1.00 result is accurate; it’s the value 3 minus 1, which is 2. In fact, the documentation for the remainder() function explains the result like this:
x-n*y
Value x
is the dividend and y
is the divisor, with n
as the result of x/y
rounded to the nearest integer. So:
29-(29/3)*3 = 29-(10)*3 = 29-30 = -1
The -1.00 result makes sense given the parameters of the remainder() function.
I still prefer the div() function to obtain a more realistic answer. But the remainder() function remains available, plus it works with real numbers and not just integers.