Code examples

Give change

Subtract the price from the amount paid to find the change.

Simple example

Start5 − 2
Result3
lesson.zig
const std = @import("std");

fn show(value: i64) !void {
    var buffer: [64]u8 = undefined;
    var writer = std.fs.File.stdout().writer(&buffer);
    try writer.interface.print("{d}\n", .{value});
    try writer.interface.flush();
}

pub fn main() !void {
    try show((5 - 2));
}
Output
3
Advertisements

Everyday examples

A customer pays 20 for a meal costing 13. How much change should they get?

Start20 − 13
Result7
lesson.zig
const std = @import("std");

fn show(value: i64) !void {
    var buffer: [64]u8 = undefined;
    var writer = std.fs.File.stdout().writer(&buffer);
    try writer.interface.print("{d}\n", .{value});
    try writer.interface.flush();
}

pub fn main() !void {
    const paid: i64 = 20;
    const price: i64 = 13;
    try show((paid - price));
}
Output
7
Run the example
Terminal
zig run lesson.zig
Official documentation