CLASS NOTES:
SD110 Computer Programming Logic
 
 

 

MODULUS
 
 
OVERVIEW:
 
   


Why would such a simple concept need a whole page to itself? Well, it probably doesn't, but it is better to start with something overly simple than overly hard at the beginning, so here we go.

The Modulus function returns the Remainder from a division equation
and is often represented by a percentage sign '%' or the word MOD when written out.

EXAMPLES:

7 % 5 = 7 MOD 5 = 7 / 5 = 1 with a remainder of 2 = 2
How many times one number divides into another is unimportant,
what remains is the value that the Modulus function returns.

Yeah, but what value is there in knowing that?

The Modulus function can help you make comparisons between numbers
to determine the properties of a number in question.

For example, determining Even versus Odd numbers:

X % 2 (X Mod 2) will always be 0 if X is Even
and if it is Not 0, then X is Odd.

Or determining a Prime Number:

If X%Y(X Mod Y) is ever 0 And the value of Y is neither 1 nor X,
Then X is not Prime. A Prime number is only evenly divisible by itself and the number 1.

(Note: We will write out code or a flowchart for Prime Numbers in a latter lesson.
This example is here just to show you some of the benefits of using the Modulus function.)

   
     
 
BACK