This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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...
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