Integer Division and Modulo
Part of the Fundamentals section of Coddy's R journey — lesson 13 of 78.
Beyond basic division, R provides two special operators for working with whole numbers: integer division and modulo.
Integer division (%/%) divides two numbers and returns only the whole number part, discarding any remainder:
17 %/% 5 # Returns 3 (not 3.4)Think of it as asking "how many complete times does 5 fit into 17?" The answer is 3 complete times.
Modulo (%%) returns the remainder after division:
17 %% 5 # Returns 2 (the remainder)This tells you what's left over after dividing 17 by 5. Since 5 goes into 17 three times (15), the remainder is 2.
These operators are incredibly useful in programming. The modulo operator is commonly used to check if a number is even or odd - if number %% 2 equals 0, the number is even. Integer division helps when you need to distribute items into groups or convert units like minutes to hours.
Challenge
EasyConvert a total number of minutes into hours and remaining minutes using integer division and modulo.
You are provided with the following variable:
total_minutes <- 197Using the integer division and modulo operators:
- Create a variable called
hoursthat stores how many complete hours are intotal_minutes - Create a variable called
remaining_minutesthat stores the leftover minutes after extracting the complete hours
Use the print() function to display hours and remaining_minutes in that exact order.
Cheat sheet
R provides two special operators for working with whole numbers:
Integer division (%/%) returns only the whole number part, discarding any remainder:
17 %/% 5 # Returns 3Modulo (%%) returns the remainder after division:
17 %% 5 # Returns 2Common use cases include checking if a number is even (number %% 2 == 0) and converting units (e.g., minutes to hours).
Try it yourself
# Given variable
total_minutes <- 197
# TODO: Write your code below
# Use integer division (%/%) to calculate complete hours
# Use modulo (%%) to calculate remaining minutes
# Output the results
print(hours)
print(remaining_minutes)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values