if number is divisible by 3 or 5 double it in matlab

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.m
function result = doubleIfDivisible(number)
    if mod(number, 3) ==0 || mod(number, 5) == 0
        result = number * 2;
    else
        result = number;
    end
end
168 chars
8 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.m
result = doubleIfDivisible(6)
30 chars
2 lines

This will return 12, since 6 is divisible by 3 and is doubled by the function.

gistlibby LogSnag