C/C++ Arena

Step 3 of 6

Remainder with %

The % operator (called modulo or remainder) gives what's left over after an integer division.

#include <stdio.h>

int main(void) {
    printf("%d %d\n", 17 / 5, 17 % 5);
    printf("%d %d\n", 20 / 5, 20 % 5);
    printf("%d\n", 9 % 2);
    return 0;
}
3 2
4 0
1

17 divided by 5 is 3 with 2 left over (3 times 5 is 15, and 17 minus 15 is 2). / gives the 3 and % gives the 2. Together they always satisfy (a / b) * b + a % b == a.

What it's good for

% only works on integers. For doubles there's a library function called fmod, but you'll rarely need it.

Your task

A match lasted 754 seconds. Print it as minutes and seconds: 12 min 34 sec.

Challenge 2

There are 23 players and each team has 5 players. Compute how many full teams you can make and how many players are left over, and print 4 full teams, 3 left over.

Challenge 3

A long match lasted 3725 seconds. Print it as hours, minutes and seconds: 1 h 2 min 5 sec. You'll need both / and %, more than once.

Previous: The integer division trap Next: Shorthand operators