Problem 42404. Divisible by 2

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

31.83% Correct | 68.17% Incorrect
Last Solution submitted on Mar 07, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers540

Suggested Problems

More from this Author139

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!