Skip to content

Instantly share code, notes, and snippets.

@gingeleski
Created January 18, 2017 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingeleski/42cb83ab4a9384203d3bbe64aaa7f092 to your computer and use it in GitHub Desktop.
Save gingeleski/42cb83ab4a9384203d3bbe64aaa7f092 to your computer and use it in GitHub Desktop.
Prints numbers 1 to 100, but for multiples of 3 prints “Bucks” instead and for multiples of 5 prints “Fizz”
public static void printBucksFizzOrNumber()
{
for (int number = 1; number <= 100; number++)
{
boolean hasThreeOrFiveAsMultiple = false;
// If number is a multiple of 3, print "Bucks" and set flag
if (number % 3 == 0)
{
hasThreeOrFiveAsMultiple = true;
System.out.print("Bucks");
}
// If number is a multiple of 5, print "Fizz" and set flag
if (number % 5 == 0)
{
hasThreeOrFiveAsMultiple = true;
System.out.print("Fizz");
}
// Just print the number if flag hasn't been set
if (false == hasThreeOrFiveAsMultiple)
{
System.out.print(number);
}
System.out.print("\n"); // Line break after applicable printing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment