FizzBuzz in Swift

Screen Shot 2016-08-17 at 10.12.51 AM

Fizz Buzz is a very popular beginner level problem in many programming languages and is also used frequently in technical interviews. It goes something like 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”.”

What trips up many  is that the question is worded to begin to replace numbers with a string when they are divisible by 3, then 5, then both 3 and 5; however to complete this problem you must take apart and rearrange the order in which the problem is written.

logically, the first “if” statement must contain the equation to find what is both divisible by 3 and 5 so that the function doesn’t get caught in finding the the numbers divisible by 3 and 5 initially and already assigning them fizz and buzz respectively.

There are many ways to solve fizzbuzz including use of a while loop, a switch statement and branching. What other ways have you found to solve fizzbuzz in Swift?

Advertisements