|
CLASS NOTES:
SD110 Computer Programming Logic |
|
MODULUS
|
OVERVIEW:
|
The Modulus function returns the Remainder from a division equation
EXAMPLES: 7 % 5 = 7 MOD 5 = 7 / 5 = 1 with a remainder of 2 = 2 Yeah, but what value is there in knowing that? The Modulus function can help you make comparisons between numbers
For example, determining Even versus Odd numbers: X % 2 (X Mod 2) will always be 0 if X is Even Or determining a Prime Number: If X%Y(X Mod Y) is ever 0 And the value of Y is neither 1 nor X, (Note: We will write out code or a flowchart for Prime Numbers in
a latter lesson. |