Skip to content

Instantly share code, notes, and snippets.

@asterwolf
Created November 22, 2015 23:27
Show Gist options
  • Save asterwolf/859cfdfd84e435d9a761 to your computer and use it in GitHub Desktop.
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.
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);
}
}
}
}
@asterwolf
Copy link
Author

The Fizz Buzz test. :)

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