Drill

ProblemsC# › network

How many pages does the result set need

easynetworkMathC#

A paginated API needs to know how many pages to render. Divide the total items by the page size and round up.

PagesTotal(items: int, perPage: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int PagesTotal(int items, int perPage) {
    
}

Worked examples

CallResult
PagesTotal(10, 3)4
PagesTotal(9, 3)3
PagesTotal(0, 5)0
PagesTotal(5, 0)0

Hint

The classic integer ceil is (items + perPage - 1) / perPage.

Reference solution in C#
public int PagesTotal(int items, int perPage) {
    if (items <= 0 || perPage <= 0) return 0;
    return (items + perPage - 1) / perPage;
}

The same problem in another language

More network problems in C#