No Nines!

Like many innocent and silly math games, No Nines is nerdy to play but fun to code. The game works like this:

Think of a positive integer. If the value is divisible evenly by nine or contains the digit ‘9’, it’s considered “illegal.” All other numbers are legal and valid. Examples:

The integer 19 is illegal because it contains a 9.
The integer 27 is illegal because it’s divisible evenly by nine.
The integer 31 is legal because it doesn’t contain a 9 and cannot be divided evenly by nine.

Obviously this game becomes more “fun” for the nerds when you specify a huge value. Oh, I get all excited thinking about jolly math geeks enjoying a round of No Nines.

As a C coder, you need never do math in your head. The computer determines whether an integer is evenly-divisible by nine or contains the digit ‘9’, which is your Exercise for this month:

Write a program that churns through integers 1 to 100. For each value, a test is made to determine whether the integer is evenly divisible by nine or contains the digit ‘9’. If so, the message “is illegal” is output along with the reason.

Here is a snippet of output from my solution:

  1 is legal
  2 is legal
  3 is legal
  4 is legal
  5 is legal
  6 is legal
  7 is legal
  8 is legal
  9 is illegal -  divisible by 9 and contains '9'
 10 is legal
 11 is legal
 12 is legal
 13 is legal
 14 is legal
 15 is legal
 16 is legal
 17 is legal
 18 is illegal - divisible by 9
 19 is illegal - contains '9'

Your task is to write code that generates similar output. Ensure that all values 1 to 100 are scanned and a list is output similar to what’s shown above. Legal numbers are flagged as such. Illegal values are flagged, along with the reason why it’s illegal: either because it’s divisible by nine, contains the digit ‘9’, or both.

Click here to view my solution, though please first try this Exercise on your own.

Leave a Reply