Write a program that computes the rounded integer division between two integers, i.e. divide and round to the nearest integer. For the purposes of this exercise, this should be done without resorting to floats or doubles. Halfway cases, where the fractional part is exactly “.5”, should be rounded to the nearest even integer. Here are a few examples:
Each line of input will contain two integers n and d in the range:
-2 000 000 000 ≤ m, n ≤ 2 000 000 000
For each line of input your program should produce a line of output with the result of rounding m divided by n.
4 3
8 3
100 80
18 12
1
3
1
2
roundiv functionYour program should be implemented using an roundiv function
that receives two integers as arguments and returns an integer.
Please refer to the information for the chosen language:
int roundiv(int n, int d);def roundiv(n,d):roundiv :: Int -> Int -> Intint roundiv(int n, int d);public static int Roundiv(int n, int d) in a public class Programpublic static int roundiv(int n, int d) in a public class Roundivfunction roundiv(n,d)function roundiv (n, d)def roundiv(n,d)For the purposes of this exercise,
you should not use floats or doubles
when implementing roundiv
nor rounding functions provided by your language of choice.
The point here is to exercise the implementation of rounding.
Submit your solution to be graded according to the following list:
roundiv functiontry next: primes
Copyright © 2020-2023 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/roundiv