Skip to content

Instantly share code, notes, and snippets.

@ykjchen
Created December 6, 2013 10:43
Show Gist options
  • Save ykjchen/7821799 to your computer and use it in GitHub Desktop.
Save ykjchen/7821799 to your computer and use it in GitHub Desktop.
Fizz Buzz in C/ObjC. The console will log each number between 1 and toValue (inclusive), substituting multiples of three with "fizz", multiples of five with "buzz", and multiples of both three and five with "fizz buzz."
/*!
* The console will log each number between 1 and toValue (inclusive),
* substituting multiples of three with "fizz", multiples of five with "buzz",
* and multiples of both three and five with "fizz buzz."
*/
void fizzBuzz(int toValue)
{
for (int i = 1; i <= toValue; i++) {
BOOL isFizz = (i % 3 == 0);
BOOL isBuzz = (i % 5 == 0);
if (isFizz && isBuzz) {
printf("fizzbuzz");
} else if (isFizz) {
printf("fizz");
} else if (isBuzz) {
printf("buzz");
} else {
printf("%i", i);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment