Created
January 18, 2017 00:58
-
-
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”
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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