Library reference

Find how many are left over

Divide into equal groups and read the remainder. All the quantities in these examples are positive whole numbers.

@rem

Simple example

Divide seven into groups of three. How many are left?

Start7 ÷ 3
Result1
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{@rem(@as(i64, 7), 3)});
}
Output
1
Advertisements

Everyday examples

A shop packs 17 apples in bags of five. How many apples are left loose?

Start17 ÷ 5
Result2
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{@rem(@as(i64, 17), 5)});
}
Output
2

Twenty learners form teams of four. How many learners still need a team?

Start20 ÷ 4
Result0
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{@rem(@as(i64, 20), 4)});
}
Output
0

A classroom has 19 crayons. How many are left after making sets of six?

Start19 ÷ 6
Result1
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{@rem(@as(i64, 19), 6)});
}
Output
1
Run the example
Terminal
zig run lesson.zig
Official documentation