cybersecurity · beginner · ~15 min · safe pentest lab

Number of ports to scan

Compute an inclusive range size.

Challenge

Compute how many ports a (loopback-only) scan would cover, so you can bound and rate-limit it.

Task

Implement int port_count(int lo, int hi) that returns the number of ports in the inclusive range [lo, hi], or 0 if hi < lo.

Input

  • lo, hi: the inclusive range bounds.

Output

Returns int: hi - lo + 1 when hi >= lo, otherwise 0.

Example

port_count(1, 1024)   ->   1024
port_count(80, 80)    ->   1
port_count(100, 10)   ->   0   (empty range)

Edge cases

  • A single-port range (lo == hi) has count 1.
  • An inverted range (hi < lo) has count 0.

Rules

  • Inclusive range; no negative counts.

Input format

Two ints: the inclusive bounds lo and hi.

Output format

An int: the inclusive range size, or 0 if hi < lo.

Constraints

Inclusive: hi - lo + 1 when hi >= lo, else 0.

Starter code

int port_count(int lo, int hi) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.