HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

FizzBuzz Problem Solution

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemsolutionfizzbuzz

Problem

I wrote this simple solution about FizzBuzz. Is there any possible way to solve or optimize the solution, such as with bitwise tricks?

public class FizzBuzz{
  public static void main(String[] args){

    for(int i = 1 ; i <= 100 ; ++i){
      if(i % (5*3) == 0){
        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);
    }
  }
}

Solution

That's a classic FizzBuzz solution. I wouldn't try to do any clever optimization — it won't make any difference to performance. One hundred iterations of anything is trivial for a computer. Furthermore, most of the time will be dominated by the output routines, which you can't do much about.

The code formatting, on the other hand, could be improved. The most glaring issue is the omission of the braces for the final else. Why make your code ugly to save a couple of bytes?

Also note a de facto formatting convention: Put a space after if and for, and before any opening { brace.

Context

StackExchange Code Review Q#74443, answer score: 15

Revisions (0)

No revisions yet.