Writing Fizz Buzz in Xcode

Fizz Buzz is a common programming challenge that I’ve seen referenced numerous times since first learning about it through Codecademy. The prompt is this:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz.”

I remember stumbling my way through the Codecademy lesson, not really understanding it but going through the motions. Since that day (back in January, I believe it was) I had completely forgotten the details of the question and the approach for solution.

Yesterday, however, I was thinking about programming challenges and my progress thus far and remembered the Fizz Buzz challenge. I decided I’d Google the details and give it a shot in Xcode1. I was able to create some working code in less than five minutes(!) and so I figured I’d share my work process with my code journal.

I’ve realized that part of learning to code is learning to break problems into tiny little pieces. When I first started, I’d want to tackle everything that once; I’d be stuck on the first line of code since I didn’t want to solve one part then have to change everything when I needed to add another part. The largest aspect of learning to code, for me, was realizing that code really does build on other code, and 99% of the time you’ll simply be able to add to existing code to create new features. The other 1%, where the code needs to be completely rewritten, is still an extremely valuable way to think about the problem.

I know this often gets repeated ad nauseum, but just start writing the code.

So, the first component of Fizz Buzz is to print the numbers from 1 to 100. Before we can even worry about multiples of three or five, we need to create a program that simply prints out 1 through 100. That’s easy, we just need a simple loop.

for (int i = 1; i <= 100; i++) { NSLog(@"%i", i);}

That’s it. Run that snippet of code and you’ll get each number between 1 and 100 printed to the console. Fantastic.

Now, it’s time to add our first exception to the rule, printing Fizz instead of each multiple of three. Another way to think about this word-replacement is: if the number is a multiple of three, print Fizz, otherwise print the number. This construct is an if/else statement.

An if/else statement is exactly what I used. Since “print the number without any modifications” is the default action if it’s not a multiple of three, that bit of code will be part of the catch-all “else” code. The calculations for multiples of three will be part of the if statement.

So how can we tell if a number is a multiple of three? My brain first snapped to an extremely complicated solution: take 3 * 1, then check that against i, then 3 * 2 calculated against i, and so on. For this, I’d need a whole new loop, and I’d need to create a way to break that loop eventually, meaning I’d need to enter j < ?? perimeter, which means the code wouldn’t be scalable.

Luckily, we have the modulo operator (%). This bit of code divides the first number by the second number and returns the remainder of the result. So:

100 % 4 = 0

Going back to Fizz Buzz, multiples aren’t just solvable through multiplication. Sure, 26 * 3 is guaranteed to be a multiple of three. This also means that if a number is evenly divided by three, it’s also a multiple of three.

100 % 3 = 1 (100 is not a multiple of three)

99 % 3 = 0 (99 is a multiple of three)

Let’s head back to the loop we already wrote and add a test for multiples of three.

If we run that code, we’ll see that every multiple of three has been replaced with the word “Fizz.” We’ll find and replace multiples of five the same way, this time with an else if statement and with the word “Buzz.”

Running this code replaces multiples of three and multiples of five. The only think left to add is the word FizzBuzz to each number that is divisible by both three and five.

If we look at our console log, we’ll see that numbers divisible by both three and five are currently printing “Fizz.” This is because our if statement for “Fizz” comes before our statement to print “Buzz.” So even though these numbers are divisible by both three and five, the program is satisfied after the first if statement and that cycle through the loop is complete, without the “else if” statement even being checked.

If we add our FizzBuzz statement after the current if or else if statement, it’ll never be read because the loop will be satisfied before we even get to the FizzBuzz test. We’ll need to add this to the very beginning, like such:

Since the FizzBuzz test is now our first if statement, we have to change the Fizz test to an else if statement.

That’s it. This program matches all the perimeters of the Fizz Buzz challenge. You just have to build it piece by piece. Fizz. Buzz.

Since I’m learning Objective-C without a background in C, I’m still mostly unclear as to the differences. As a result, I’m not sure if the code I wrote qualified as Objective-C or could just be considered plain C. I know that it doesn’t really matter, since Xcode compiles both Objective-C and C, but I figured I’d write “give it a shot in Xcode” as to avoid referring to the programming language incorrectly. ↩

Comments

there is a semantic typo in:
“Luckily, we have the modulo operator (%). This bit of code divides the first number by the second number and returns the result. So:”
it should read
“returns the remainder of their result. So:”
love your blog btw.
on a side note, ever consider a WYSIWYG text editor for your comments section?
also the text input boxes in this comment frame get truncated as the main box expands. I can’t hit the post comment button without some hacking :/

Good catch and thanks for the kind words about my blog. I’ve fixed the mistake.

Also, great idea about a WYSIWYG commenting system, didn’t even think of that. I’ve been contemplating switching to disqus for a while, so I’ll have to see if they support wysiwyg formatting. That’ll be very helpful for my code-related posts. Thanks.