Created
November 22, 2015 23:27
-
-
Save asterwolf/859cfdfd84e435d9a761 to your computer and use it in GitHub Desktop.
A program that counts from 1 to 100, printing "Fizz" if the number is divisible by 3, prints "Buzz" if it's divisible by 5, and prints "FizzBuzz" if it's divisible of both 3 and 5.
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 class FizzBuzz{ | |
public static void main(String[] args){ | |
for(int i = 1; i <= 100; i++){ | |
/* | |
* Could check to see if i is divisible by 5 and 3 first | |
* that way you won't have to check if it's divisible by 3 | |
* and not also divisible by 5 and vice versa. | |
if(i % 3 == 0 && i % 5 != 0){ | |
System.out.println("Fizz"); | |
} | |
else if(i % 5 == 0 && i % 3 != 0){ | |
System.out.println("Buzz"); | |
} | |
else if(i % 5 == 0 && i % 3 == 0){ | |
System.out.println("FizzBuzz"); | |
} | |
else{ | |
System.out.println(i); | |
} | |
*/ | |
if(i % 15 == 0){ //if divisible of 15, it's divisible by 3 and 5 | |
System.out.println("FizzBuzz"); | |
} | |
else if(i % 3 == 0){ | |
System.out.println("Fizz"); | |
} | |
else if(i % 5 == 0){ | |
System.out.println("Buzz"); | |
} | |
else{ | |
System.out.println(i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Fizz Buzz test. :)