basics · beginner · ~15 min

Apply an operator

Branch on an operator and guard division.

Challenge

Implement int apply_op(int a, int b, char op) for op in '+', '-', '*', '/'. For division by zero (or an unknown op) return 0.

Starter code

int apply_op(int a, int b, char op) {
    /* TODO */
    return 0;
}

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