Menu
Coddy logo textTech

The Modulo Operator

Part of the Fundamentals section of Coddy's GO journey — lesson 17 of 109.

The modulo operator (%) returns the remainder after division of one number by another.

Calculate the remainder when dividing 10 by 3:

a := 10
b := 3
remainder := a % b
fmt.Println(remainder)

Output:

1

This is useful for determining if a number is even or odd:

number := 15
isEven := number % 2 == 0
fmt.Println("Is", number, "even?", isEven)

Output:

Is 15 even? false
challenge icon

Challenge

Beginner

In this challenge, you'll practice using the modulo operator (%) in Go. The modulo operator returns the remainder after division.

We have two numbers already defined. Your task is to calculate the remainder when number is divided by divisor using the modulo operator, and store it in the remainder variable.

Cheat sheet

The modulo operator (%) returns the remainder after division of one number by another.

a := 10
b := 3
remainder := a % b
fmt.Println(remainder) // Output: 1

Useful for determining if a number is even or odd:

number := 15
isEven := number % 2 == 0
fmt.Println("Is", number, "even?", isEven) // Output: Is 15 even? false

Try it yourself

package main

import "fmt"

func main() {
	// These variables are already defined for you
	number := 17
	divisor := 5
	
	// TODO: Calculate the remainder when 'number' is divided by 'divisor'
	// using the modulo operator (%) and store it in the 'remainder' variable
	var remainder int
	
	// This will print the result
	fmt.Println("The remainder when", number, "is divided by", divisor, "is:", remainder)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals