Explore BrainMass

Integer Division and Modulus to Isolate Digits in an Integer

Given a large number we sometimes need to extract individual digits or sequences of digits from it. For example, given a four-digit year (2010) we may need to extract just the two-digit year or the century. The mathematical operators modulus (Java: %) and division (Java: integer division /) help us with this. Using the operators % and / write the Java expression that will extract the following:
From 2010 extract the 10 and the 20
From 98765432 extract the middle three digits (765)
From 153 extract each individual digit (1, 5, 3)
Write your solution in a simple text file.

© BrainMass Inc. brainmass.com July 18, 2018, 2:59 am ad1c9bdddf

Solution Preview

The % and / operators will help us isolate individual digits out of the larger number. The % operator will return the remainder after doing the division. If we use the modulus operator with a power of 10 (i.e., 10, 100, 1000, etc.) then the result will be ...

Solution Summary

This solution explains how to use the % and / operators to isolate individual digits in an integer. These examples help to solidify the arithmetic concepts behind these two operators. 178 words.