programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Divisibity Test Question

Author
Posts: 201
First, my C++ compiler is on my Office machine not Java (java is on other computers) so the code below is in C++ but the question I have is relevent in Java as well...

Divisibility test here 3 & 9

Now it works just fine however, my student are doing this (Getting the same answers)

Question:

1. Why do I see books use the first solution when the second is shorter and give the same answer.
2. What is the point of all of those "tests" when the second solution appears to always work.
3. Were would the second solution fail? I want them to do the first solution not the second...

Move to appropriate forum if needed. jc

lowercase baba
Posts: 12893
63
it may depend on the exact wording of the problem. it's well known that if a number is divisible by 3, if you add up all the digits, that too will be divisible by 3. The same applies for 9. It looks to me like the first solution is a code representation of this.

if the original problem said "If a number is divisible by 3, then... Write some code to do this and print if the number is divisible by 3 and 9", then you'd have to have something like your first example.