Cody

Problem 42404. Divisible by 2

Created by goc3 in Divisible by x

This is the first problem in a set of "divisible by x" problems. You will be provided a number as a string and the function you write must return whether or not that number is divisible by x.

Rather than allowing you to use an easy way out, such as the modulus function, division and checking the result against its rounded value, Java functions, etc., you get to learn and apply integer divisibility rules. For reference, some divisibility conditions for integers are listed here. Other such references are also available elsewhere on the internet.

There are two benefits to approaching divisibility in this manner: first, you learn neat divisibility tricks that you can use in your head or on paper for large numbers and, second, you program a routine capable of determining divisibility for arbitrarily large numbers, for which existing precision fails. So, to begin...

Write a function to determine if a number is divisible by 2. This will be the case if its last digit is even.

(This problem blocks a range of functions in the first test case so that you must use one of the supplied methods; some of these restrictions will be lifted for later problems in the series.)

Next problem: divisible by 3.

Solution Stats

39.83% Correct | 60.17% Incorrect
Last solution submitted on Jan 19, 2018

Problem Comments

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!