Skip to main content
Engineering LibreTexts

2.5: Dividing Integers

  • Page ID
    117527
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    Learning Objectives

    By the end of this section you should be able to

    • Evaluate expressions that involve floor division and modulo.
    • Use the modulo operator to convert between units of measure.

    Division and modulo

    Python provides two ways to divide numbers:

    • True division (/) converts numbers to floats before dividing. Ex: 7 / 4 becomes 7.0 / 4.0, resulting in 1.75.
    • Floor division (//) computes the quotient, or the number of times divided. Ex: 7 // 4 is 1 because 4 goes into 7 one time, remainder 3. The modulo operator (%) computes the remainder. Ex: 7 % 4 is 3.

    Note: The % operator is traditionally pronounced "mod" (short for "modulo"). Ex: When reading 7 % 4 out loud, a programmer would say "seven mod four."

    Checkpoint: Quotient and remainder
    Concepts in Practice \(\PageIndex{1}\): Division and modulo

    What is the value of this expression?

    13 / 5

    1. 2
    2. 2.6
    3. 3
    Answer

    b. 13 divided by 5 is 2 and 3/5. The / operator computes the result as a float.

    Concepts in Practice \(\PageIndex{2}\): Division and modulo

    What is the value of this expression?

    13 % 5

    1. 2
    2. 2.6
    3. 3
    Answer

    c. 5 goes into 13 two times, remainder 3. The % operator computes the remainder.

    Concepts in Practice \(\PageIndex{3}\): Division and modulo

    What is the value of this expression?

    1 // 4

    1. 0
    2. 0.25
    3. 1
    Answer

    a. 4 goes into 1 zero times, remainder 1. The // operator computes the quotient.

    Concepts in Practice \(\PageIndex{4}\): Division and modulo

    What is the value of this expression?

    2 % 0

    1. 0
    2. 2
    3. Error
    Answer

    c. Dividing by zero is undefined in math. Python raises an error when dividing by zero.

    Unit conversions

    Division is useful for converting one unit of measure to another. To convert centimeters to meters, a variable is divided by 100. Ex: 300 centimeters divided by 100 is 3 meters.

    Amounts often do not divide evenly as integers. 193 centimeters is 1.93 meters, or 1 meter and 93 centimeters. A program can use floor division and modulo to separate the units:

    • The quotient, 1 meter, is 193 // 100.
    • The remainder, 93 centimeters, is 193 % 100.

    Programs often use floor division and modulo together. If one line of code floor divides by m, the next line will likely modulo by m. The unit m by which an amount is divided is called the modulus. Ex: When converting centimeters to meters, the modulus is 100.

    Checkpoint: Money and time
    Concepts in Practice \(\PageIndex{5}\): Unit conversions

    What is the modulus for converting minutes to hours?

    1. 40
    2. 60
    3. 280
    Answer

    b. The modulus is the amount to divide by. To convert hours to minutes, a program divides by 60.

    Concepts in Practice \(\PageIndex{6}\): Unit conversions

    A program has the line pounds = ounces // 16. What is likely the next line of code?

    1. ounces = ounces % 16
    2. pounds = ounces % 16
    3. ounces = ounces - pounds * 16
    Answer

    a. Programs that use floor division often use modulo on the next line.

    Try It: Arrival time

    Having a mobile device can be a lifesaver on long road trips. Programs like Google Maps find the shortest route and estimate the time of arrival. The time of arrival is based on the current time plus how long the trip will take.

    Write a program that (1) inputs the current time and estimated length of a trip, (2) calculates the time of arrival, and (3) outputs the results in hours and minutes. Your program should use the following prompts (user input in bold):

    13
        Current minute (0-59)? 25
        Trip time (in minutes)? 340
        Current hour (0-23)? 13
        Current minute (0-59)? 25
        Trip time (in minutes)? 340

    In this example, the current time is 13:25 (1:25pm). The trip time is 340 minutes (5 hours and 40 minutes). 340 minutes after 13:25 is 19:05 (7:05pm). Your program should output the result in this format:

        Arrival hour is 19
        Arrival minute is 5

    The arrival hour must be between 0 and 23. Ex: Adding 120 minutes to 23:00 should be 1:00, not 25:00. The arrival minute must be between 0 and 59. Ex: Adding 20 minutes to 8:55 should be 9:15, not 8:75.

    Hint: Multiply the current hour by 60 to convert hours to minutes. Then, calculate the arrival time, in total minutes, as an integer.

    Your code must not use Python keywords from later chapters, such as if or while. The solution requires only addition, multiplication, division, and modulo.

    Interactive Code
    # Prompt for inputs.
    hour = int(input("Current hour (0-23)? "))
    minute = int(input("Current minute (0-59)? "))
    add = int(input("Trip time (in minutes)? "))
    
    # TODO: Calculate the time.
    hour = 0
    minute = 0
    
    # Display the results.
    print()
    print("Arrival hour is", hour)
    print("Arrival minute is", minute)
    
     
    Input

    13
    25
    340

    Answer

    # prompt for inputs
    hour = int(input("Current hour (0-23)? "))
    minute = int(input("Current minute (0-59)? "))
    add = int(input("Trip time (in minutes)? "))

    # calculate the time
    result = hour * 60 + minute + add
    hour = result // 60 % 24
    minute = result % 60

    # display the results
    print()
    print("Arrival hour is", hour)
    print("Arrival minute is", minute)

    Try It: Change machine

    Self-checkout aisles are becoming increasingly popular at grocery stores. Customers scan their own items, and a computer determines the total purchase amount. Customers who pay in cash insert dollar bills, and a machine automatically dispenses change in coins.

    That's where this program comes into the story. Your task is to calculate how many of each coin to dispense. Your program should use the following prompts (user input in bold):

        Total amount? 18.76
        Cash payment? 20

    You may assume that the cash paid will always be a whole number (representing dollar bills) that is greater than or equal to the total amount. The program should calculate and output the amount of change due and how many dollars, quarters, dimes, nickels, and pennies should be dispensed:

        Change Due $1.24
        
         Dollars: 1
        Quarters: 0
           Dimes: 2
         Nickels: 0
         Pennies: 4

    Hint: Calculate the total change, in cents, as an integer. Use the round() function to avoid floating-point errors.

    Your code must not use Python keywords from later chapters, such as if or while. The solution requires only subtraction, multiplication, division, and modulo.

    Interactive Code
    # Read the input.
    total = float(input("Total amount? "))
    cash = int(input("Cash payment? "))
    
    # TODO: Calculate change.
    change = 0
    print()
    print("Change due $", change)
    
    # TODO: Print the results.
    print()
    print("  Dollars:", 0)
    print(" Quarters:", 0)
    print("    Dimes:", 0)
    print("  Nickels:", 0)
    print("  Pennies:", 0)
    
     
    Input

    18.76
    20

    Answer

    # Read the input.
    total = float(input("Total amount? "))
    cash = int(input("Cash payment? "))

    # Calculate change.
    change = round(cash - total, 2)
    print()
    print("Change due $", change)
    change = int(change * 100)

    # Print the results.
    print()
    print(" Dollars:", change // 100)
    change = change % 100
    print(" Quarters:", change // 25)
    change = change % 25
    print(" Dimes:", change // 10)
    change = change % 10
    print(" Nickels:", change // 5)
    change = change % 5
    print(" Pennies:", change)


    This page titled 2.5: Dividing Integers is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.