Saturday, February 15, 2025
HomeC ProgrammingSeeing What’s Left Over with Division

Seeing What’s Left Over with Division


Studying division at school means lengthy division. The method includes a quotient and a the rest. For instance, 42÷8 works out to five (quotient) with 2 the rest. On a pc, nonetheless, division renders the consequence as 5.25. So how do you get the rest separated?

Earlier than rolling headlong into dreaded math, think about a assessment of phrases as offered in Determine 1.

Determine 1. Division and its jargon.

With division, you might have two values: The numerator or dividend, and the denominator or divisor. The numerator is on high. The denominator, like all demons, goes under. (That’s how I bear in mind it.) The “gazinta” format for division can also be proven in Determine 1 (merchandise C). That is the shape taught first at school, which is why the rest turns into a factor.

In programming, the / operator handles division. In my C programming books, I like to recommend utilizing float or double values to acquire the correct outcomes. Once you use integers, the result’s truncated.

When you want to find the rest, you’ll be able to pull just a few methods. The primary one is from a weblog put up on the div() operate, which returns a construction containing the quotient and the rest of a division operation. Once you simply have to know the rest, nonetheless, you need to use the aptly-named the rest() operate. Right here’s its man web page definition:

double the rest(double x, double y)

This operate requires together with the math.h header file. In Linux, you need to specify the -lm change when compiling to hyperlink within the math library.

The next code demonstrates the the rest() operate.

2024_10_26-Lesson.c


#embody <stdio.h>
#embody <math.h>

int essential()
{
    double dividend,divisor,r;

    printf("Enter the dividend (numerator): ");
    scanf("%lf",&dividend);
    printf("Enter the divisor (denominator): ");
    scanf("%lf",&divisor);

    r = the rest(dividend,divisor);
    printf("%.2f/%.2f = %.2f with %.2f remaindern",
            dividend,
            divisor,
            dividend/divisor,
            r
          );

    return 0;
}

The operate prompts for 2 double values, that are then thrust into the the rest() operate:

r = the rest(dividend,divisor);

A printf() assertion outputs the outcomes, which embody commonplace division to acquire the exact worth. However the worth of variable r is what’s left over — the rest. Right here’s a pattern run:

Enter the dividend (numerator): 87.0
Enter the divisor (denominator): 6.0
87.00/6.00 = 14.50 with 3.00 the rest

This take a look at run threw me:

Enter the dividend (numerator): 29.0
Enter the divisor (denominator): 3.0
29.00/3.00 = 9.67 with -1.00 the rest

The results of 29 &div; 3 is 9 with 2 remaining. However the -1.00 result’s correct; it’s the worth 3 minus 1, which is 2. The truth is, the documentation for the the rest() operate explains the consequence like this:

x-n*y

Worth x is the dividend and y is the divisor, with n as the results of x/y rounded to the closest integer. So:

29-(29/3)*3 = 29-(10)*3 = 29-30 = -1

The -1.00 consequence is sensible given the parameters of the the rest() operate.

I nonetheless desire the div() operate to acquire a extra life like reply. However the the rest() operate stays obtainable, plus it really works with actual numbers and never simply integers.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments