To check if a number is divisible by 3 or 5, we can use the modulo operator %
to determine the remainder of the number when divided by 3 or 5. If the remainder is 0, then the number is divisible by 3 or 5.
To double the number if it is divisible by 3 or 5, we can use an if statement to check if the number satisfies the condition, and then use the arithmetic operator *
to double the number.
Here is an example code block that defines a function doubleIfDivisible
that takes a number as input, and returns the doubled number if it is divisible by 3 or 5, and the original number otherwise:
main.m168 chars8 lines
In this code, mod(number, 3)
and mod(number, 5)
compute the remainder of number
when divided by 3 and 5, respectively. The ||
operator checks if either condition is true. If number
is divisible by 3 or 5, the statement result = number * 2
doubles the value of number
, otherwise the original value is returned.
You can call this function using a number as input, like this:
main.m30 chars2 lines
This will return 12
, since 6 is divisible by 3 and is doubled by the function.
gistlibby LogSnag