index | submit | rank | book

roundiv – Division of integers with rounding

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:

Input and Output

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.

Example input

4 3
8 3
100 80
18 12

Example output


The roundiv function

Your 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:

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:

try first: gcd lcm

try next: primes

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
All rights reserved