Skip to content

Instantly share code, notes, and snippets.

@abijlani
Last active August 29, 2015 14:02
Show Gist options
  • Save abijlani/f54e95c0937d960fb7cd to your computer and use it in GitHub Desktop.
Save abijlani/f54e95c0937d960fb7cd to your computer and use it in GitHub Desktop.
FizzBuzz in Swift
/*
A FizzBuzz written in Swift
Problem: Write a program that prints the numbers from 1 to 20. 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".
*/
func isMultipleOf(number: Int, multiple: Int) -> Bool {
return (number % multiple) == 0
}
func getFizzBuzz (number: Int) -> String {
if ( isMultipleOf(number,3) && isMultipleOf(number,5)){
return "FizzBuzz"
} else if (isMultipleOf(number,3)) {
return "Fizz"
} else if (isMultipleOf(number,5)) {
return "Buzz"
} else {
return String(number)
}
}
for i in 1..21 {
println(getFizzBuzz(i))
}
@abijlani
Copy link
Author

abijlani commented Jun 5, 2014

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment