File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Divisibity Test Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Divisibity Test Question" Watch "Divisibity Test Question" New topic

Divisibity Test Question

James Chegwidden
Ranch Hand

Joined: Oct 06, 2002
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)


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
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Divisibity Test Question
jQuery in Action, 3rd edition