Skip to content
Exercise · Functions · lesson 9 of 10

Exercise: quotient and remainder

Write a function func divMod(a, b int) (int, int) that returns the quotient and the remainder of dividing a by b as mathematics defines them. The quotient q and the remainder r satisfy a == q*b + r, and the remainder lies in the range 0 <= r < b.

  • divMod(17, 5) → 3, 2
  • divMod(-17, 5) → -4, 3
  • divMod(10, 5) → 2, 0

Constraints: a is from -1000 to 1000 and may be negative or zero, b is from 1 to 100; when a is divisible by b, the remainder is 0.

Do not add extra imports unless the task says so explicitly.

divmod.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way