Skip to content

Instantly share code, notes, and snippets.

@hanaori
Created March 24, 2015 14:19
Show Gist options
  • Save hanaori/6f86c0cdf6d0fbaf5a74 to your computer and use it in GitHub Desktop.
Save hanaori/6f86c0cdf6d0fbaf5a74 to your computer and use it in GitHub Desktop.
FizzBuzz without using modulo "%"
public class FizzBuzz {
public void getResultOfFizzBuzz(){
for(int i = 1; i <= 100; i++){
if(calcRemainder(i, 3) == 0 && calcRemainder(i, 5) == 0){
System.out.println("FizzBuzz");
}else if(calcRemainder(i, 3) == 0){
System.out.println("Fizz");
}else if(calcRemainder(i, 5) == 0){
System.out.println("Buzz");
}else{
System.out.println(i);
}
}
}
private int calcRemainder(int num, int divisor){
while(num >= divisor){
num = num - divisor;
}
return num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment